<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Question about #define in CodeWarrior for MCU</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190085#M6793</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hi Felix&lt;BR /&gt;IMHO the compiler is right. The #define statement just defines the parameter as whatever you put after it - it could be a value, or a calculation or a function call or piece of text - and that is just substituted into the code when you use that parameter.&lt;BR /&gt;&lt;BR /&gt;In this case you told it that MAX_BUFFER_LENGTH was "10;"&lt;BR /&gt;So the line became:&lt;BR /&gt;if (x + MAX_BUFFER_LENGTH&lt;IMG alt=":smileywink:" class="emoticon emoticon-smileywink" id="smileywink" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif" title="Smiley Wink" /&gt;&lt;BR /&gt;i.e. semi colon in the wrong place = missing closing bracket ")"&lt;BR /&gt;&lt;BR /&gt;You could have put:&lt;BR /&gt;#define MAX_BUFFER_LENGTH (5 * 2)&lt;BR /&gt;this would have worked, the compiler would have done the maths and put the answer (10) into the if statement.&lt;BR /&gt;&lt;BR /&gt;Hope that all makes sense. You can do some really complicated things with #defines - but my personal experience is that it's best to keep it simple. :smileyhappy:&lt;BR /&gt;&lt;BR /&gt;Good luck with your programming&lt;BR /&gt;Steve&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 01 Oct 2008 13:30:51 GMT</pubDate>
    <dc:creator>Stephen</dc:creator>
    <dc:date>2008-10-01T13:30:51Z</dc:date>
    <item>
      <title>Question about #define</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190084#M6792</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;SPAN&gt;Hi&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Just another dumb question:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am using Codewarrior 6.2 Release, build 8127, and targeting a HC908AP32.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I try to compile the folowing code i get a "C2801: ')' missing" error, pointing at the line "if (x + MAX_BUFFER_LENGTH)" in the foo() function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;DIV class="msg_source_code"&gt;&lt;DIV class="text_smallest"&gt;Code:&lt;/DIV&gt;&lt;PRE&gt;#include &amp;lt;hidef.h&amp;gt; /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */void MCU_init(void); /* Device initialization function declaration */#define MAX_BUFFER_LENGTH 10;unsigned char x;void foo(void){&amp;nbsp; if (x + MAX_BUFFER_LENGTH)&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp; x++; // do something&amp;nbsp; }}void main(void) {&amp;nbsp; MCU_init(); /* call Device Initialization */&amp;nbsp; foo();&amp;nbsp; for(;;)&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp; } }&lt;/PRE&gt;&lt;/DIV&gt;&lt;BR /&gt;My original code was a little different and I lost some time until i realized that the semicolon at the end of the line:&lt;BR /&gt;&lt;PRE&gt;define MAX_BUFFER_LENGTH 10;&amp;lt;---
&lt;/PRE&gt;was the problem.&lt;BR /&gt;&lt;BR /&gt;My question is, if the compiler is working correctly or is it supposed&amp;nbsp; to warn that there is a&amp;nbsp; misplaced semicolon? Just wondering because i lost almost 30 minutes until i found the error.&lt;BR /&gt;Thanks in advance&lt;BR /&gt;Regards&lt;BR /&gt;Felix&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Message Edited by nfxs on &lt;/SPAN&gt;&lt;SPAN class="date_text"&gt;2008-10-01&lt;/SPAN&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;SPAN class="time_text"&gt;02:45 AM&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Message Edited by nfxs on &lt;/SPAN&gt;&lt;SPAN class="date_text"&gt;2008-10-01&lt;/SPAN&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;SPAN class="time_text"&gt;02:46 AM&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 09:41:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190084#M6792</guid>
      <dc:creator>nfxs</dc:creator>
      <dc:date>2020-10-29T09:41:35Z</dc:date>
    </item>
    <item>
      <title>Re: Question about #define</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190085#M6793</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hi Felix&lt;BR /&gt;IMHO the compiler is right. The #define statement just defines the parameter as whatever you put after it - it could be a value, or a calculation or a function call or piece of text - and that is just substituted into the code when you use that parameter.&lt;BR /&gt;&lt;BR /&gt;In this case you told it that MAX_BUFFER_LENGTH was "10;"&lt;BR /&gt;So the line became:&lt;BR /&gt;if (x + MAX_BUFFER_LENGTH&lt;IMG alt=":smileywink:" class="emoticon emoticon-smileywink" id="smileywink" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif" title="Smiley Wink" /&gt;&lt;BR /&gt;i.e. semi colon in the wrong place = missing closing bracket ")"&lt;BR /&gt;&lt;BR /&gt;You could have put:&lt;BR /&gt;#define MAX_BUFFER_LENGTH (5 * 2)&lt;BR /&gt;this would have worked, the compiler would have done the maths and put the answer (10) into the if statement.&lt;BR /&gt;&lt;BR /&gt;Hope that all makes sense. You can do some really complicated things with #defines - but my personal experience is that it's best to keep it simple. :smileyhappy:&lt;BR /&gt;&lt;BR /&gt;Good luck with your programming&lt;BR /&gt;Steve&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Oct 2008 13:30:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190085#M6793</guid>
      <dc:creator>Stephen</dc:creator>
      <dc:date>2008-10-01T13:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: Question about #define</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190086#M6794</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Welcome the the wonderful world of macro debugging. The compiler is working correctly, try compiling this code:&lt;BR /&gt;&lt;BR /&gt;if (x + 10&lt;IMG alt=":smileywink:" class="emoticon emoticon-smileywink" id="smileywink" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif" title="Smiley Wink" /&gt;&lt;BR /&gt;&lt;BR /&gt;I bet you get the very same error.&lt;BR /&gt;&lt;BR /&gt;Painful debugging is just one of the almost countless number of reasons why one should avoid the pre-processor as far as possible.&lt;BR /&gt;&lt;BR /&gt;Use constants instead of #defines, write real functions instead of function-like macros etc etc.&lt;BR /&gt;&lt;BR /&gt;Message Edited by Lundin on &lt;SPAN class="date_text"&gt;2008-10-01&lt;/SPAN&gt; &lt;SPAN class="time_text"&gt;08:31 AM&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Oct 2008 13:31:00 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190086#M6794</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2008-10-01T13:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: Question about #define</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190087#M6795</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Nice one Freescale!&lt;BR /&gt;In my above reply, anywhere there is a graphic of a winking face, it should have the text semi-colon closing-bracket like "; )" - otherwise it won't make sense. Sorry about that!&lt;BR /&gt;Steve&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Oct 2008 13:33:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190087#M6795</guid>
      <dc:creator>Stephen</dc:creator>
      <dc:date>2008-10-01T13:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: Question about #define</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190088#M6796</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;The compiler is right, the question is more if the compiler could warn if the user does unintentionally&lt;BR /&gt;add a semicolon to the end of a macro (or miscounts the number of closing braces, or does any of the other countless many errors you can do with macros).&lt;BR /&gt;The problem is that semicolons are legal, and there are real usages where macros must end with a semicolon. I don't see any reasonable way of detecting this case without also causing wrong warnings, warnings for code for which the semicolon was intentional :smileysad:.&lt;BR /&gt;&lt;BR /&gt;I did actually never see any compiler issue a warning for such a added semicolon, in the end, C has a number of traps, for some the compiler can warn and for others the compiler cannot.&lt;BR /&gt;&lt;BR /&gt;Daniel&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Oct 2008 13:57:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190088#M6796</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2008-10-01T13:57:09Z</dc:date>
    </item>
    <item>
      <title>Re: Question about #define</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190089#M6797</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;In both C and C++ macros are simple string substitutions that are performed by the C preprocessor, which is also responsible for processing trigraphs, #include's, #if's, concatenating line continuation, and replacing comments with white space. &amp;nbsp; In some implementations the preprocessor is a separate program that is run before the compiler, and whether or not it really is a separate program in a given implementation, you should think of it that way because that is the way it works.&lt;BR /&gt;&lt;BR /&gt;This is old, but if you want to see how truely the preprocessor is disconnected from C itself, look at this web site:&lt;BR /&gt;&lt;A href="http://www.cs.tut.fi/~jkorpela/html/cpre.html" target="test_blank"&gt;http://www.cs.tut.fi/~jkorpela/html/cpre.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;-Preston&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 01 Oct 2008 19:51:56 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190089#M6797</guid>
      <dc:creator>scopuli</dc:creator>
      <dc:date>2008-10-01T19:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: Question about #define</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190090#M6798</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hi, all&lt;BR /&gt;Thanks for the answers.&lt;BR /&gt;Now i get it. I used macros only very little, so i was not aware of all its potential.&lt;BR /&gt;Now i will use it a little more careful &lt;IMG alt=":smileyvery-happy:" class="emoticon emoticon-smileyvery-happy" id="smileyvery-happy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-very-happy.gif" title="Smiley Very Happy" /&gt;&lt;BR /&gt;Regards&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 02 Oct 2008 06:48:22 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Question-about-define/m-p/190090#M6798</guid>
      <dc:creator>nfxs</dc:creator>
      <dc:date>2008-10-02T06:48:22Z</dc:date>
    </item>
  </channel>
</rss>

