<?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: #define verse constant declarations in Classic/Legacy CodeWarrior</title>
    <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124848#M49</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Hello,&lt;/DIV&gt;&lt;DIV&gt;Yes, you are correct, #DEFINE only replace the value at compilation.&lt;/DIV&gt;&lt;DIV&gt;As it's only copy of "text" as is, there is no impact of the space. It just saves you writting the same thing other and other again and allows to change a value easily impacting the whole software.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;#DEFINE&lt;/STRONG&gt; does not declare a variable.&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;Const&lt;/STRONG&gt; does not declare a variable either on its own.&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;Using Const in conjunction&lt;/STRONG&gt; with variable declaration puts it in ROM instead of RAM. (by default, see your PRM file in CW).&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Cheers,&lt;/DIV&gt;&lt;DIV&gt;Alban.&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 26 Jul 2006 17:25:23 GMT</pubDate>
    <dc:creator>Alban</dc:creator>
    <dc:date>2006-07-26T17:25:23Z</dc:date>
    <item>
      <title>#define verse constant declarations</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124845#M46</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Is there a benefit to using one or the other? I have always used #define but have read in O'reilly book that const is preferred for following scope rules... any feelings eitherway?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jul 2006 00:46:38 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124845#M46</guid>
      <dc:creator>mjcoury</dc:creator>
      <dc:date>2006-07-26T00:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: #define verse constant declarations</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124846#M47</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Const is valuable because it informs the compiler that the "variable" that you're declaring isn't. This information can be used to:&lt;BR /&gt;&lt;BR /&gt;1) Locate the constant value in firmware where it belongs instead of RAM, and&lt;BR /&gt;&lt;BR /&gt;2) The optimizer, knowing that a variable is actually a constant value, can apply optimization techniques to reduce code size.&lt;BR /&gt;&lt;BR /&gt;Just my two plutons worth.&lt;BR /&gt;&lt;BR /&gt;---Tom&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jul 2006 03:04:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124846#M47</guid>
      <dc:creator>J2MEJediMaster</dc:creator>
      <dc:date>2006-07-26T03:04:35Z</dc:date>
    </item>
    <item>
      <title>Re: #define verse constant declarations</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124847#M48</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Yes that makes sense... am I correct in stating that a #define replaces values at the time of compile so would consume even less space since it would not require any storage at all?&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jul 2006 03:16:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124847#M48</guid>
      <dc:creator>mjcoury</dc:creator>
      <dc:date>2006-07-26T03:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: #define verse constant declarations</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124848#M49</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;Hello,&lt;/DIV&gt;&lt;DIV&gt;Yes, you are correct, #DEFINE only replace the value at compilation.&lt;/DIV&gt;&lt;DIV&gt;As it's only copy of "text" as is, there is no impact of the space. It just saves you writting the same thing other and other again and allows to change a value easily impacting the whole software.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;#DEFINE&lt;/STRONG&gt; does not declare a variable.&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;Const&lt;/STRONG&gt; does not declare a variable either on its own.&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;Using Const in conjunction&lt;/STRONG&gt; with variable declaration puts it in ROM instead of RAM. (by default, see your PRM file in CW).&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Cheers,&lt;/DIV&gt;&lt;DIV&gt;Alban.&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Jul 2006 17:25:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124848#M49</guid>
      <dc:creator>Alban</dc:creator>
      <dc:date>2006-07-26T17:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: #define verse constant declarations</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124849#M50</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;mjcoury wrote:&lt;BR /&gt;Yes that makes sense... am I correct in stating that a #define replaces values at the time of compile so would consume even less space since it would not require any storage at all?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;BR /&gt;Oh, it still requires storage.&amp;nbsp; It just&amp;nbsp;stores&amp;nbsp;it in Flash amongst the code.&amp;nbsp; The #define statement is a pre-compiler directive.&amp;nbsp; Technically, any line that begins with a # is something for the pre-compiler to act on.&amp;nbsp; The pre-compiler will replace all instances of the defined token with its definition.&amp;nbsp; So doing this:&lt;/P&gt;&lt;P&gt;#define DELAY 40&lt;BR /&gt;for (i=0;i&amp;lt;DELAY;i++) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (j=0;j&amp;lt;DELAY;j++) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; asm NOP;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;is exactly the same as this (as far as the compiler is concerned):&lt;/P&gt;&lt;P&gt;for (i=0;i&amp;lt;40;i++) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (j=0;j&amp;lt;40;j++) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; asm NOP;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;When the compiler generates machine code, it will see the number 40 and use&amp;nbsp;the immediate addressing mode in order to compare with the accumulator.&amp;nbsp; The number 40 will be stored in the code as many times as you are referencing it.&amp;nbsp; In this case it is twice.&amp;nbsp; Here is the assembly generated by CodeWarrior Ver5:&lt;/P&gt;&lt;P&gt;&lt;SPAN class="msg_source_code"&gt;&lt;SPAN class="text_smallest"&gt;Code:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;    7:    char i,j;    8:    for (i=0;i&amp;lt;DELAY;i++) {  0002 95       [2]             TSX     0003 7f       [2]             CLR   ,X  0004          [5]     L4:         9:      for (j=0;j&amp;lt;DELAY;j++) {  0004 6f01     [3]             CLR   1,X  0006          [5]     L6:        10:        asm NOP;  0006 9d       [1]             NOP     0007 6c01     [4]             INC   1,X  0009 e601     [3]             LDA   1,X  000b a128     [2]             CMP   #40  ;&amp;lt;---- notice opcode a1 and immediate constant 40, which is $28 in hexadecimal  000d 25f7     [3]             BCS   L6  000f 7c       [3]             INC   ,X  0010 f6       [2]             LDA   ,X  0011 a128     [2]             CMP   #40  ;&amp;lt;---- and here it is again.  0013 25ef     [3]             BCS   L4   11:      }   12:    }   13:  }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's really up to the compiler though, how to handle it.&amp;nbsp; I tried defining a 'const char' global variable and CodeWarrior generated exactly the same code as above.&amp;nbsp; So either way you do it, the compiler has the final say - but you can switch off certain optimizations if you want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 27 Jul 2006 01:12:00 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124849#M50</guid>
      <dc:creator>rhinoceroshead</dc:creator>
      <dc:date>2006-07-27T01:12:00Z</dc:date>
    </item>
    <item>
      <title>Re: #define verse constant declarations</title>
      <link>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124850#M51</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Actually there are three ways of defining such constants,&lt;BR /&gt;- #defines&lt;BR /&gt;- enums&lt;BR /&gt;- const variables.&lt;BR /&gt;&lt;BR /&gt;There are pros and cons for all of them&lt;BR /&gt;&lt;BR /&gt;#defines&lt;BR /&gt;Bad:&lt;BR /&gt;- does not follow scoping rules.&lt;BR /&gt;E.g.&lt;BR /&gt;a.h:&lt;BR /&gt;#define count 10&lt;BR /&gt;b.c&lt;BR /&gt;#include "c.h"&lt;BR /&gt;void fun(void) {&lt;BR /&gt;int count;&lt;BR /&gt;for (count = 0; count 7; count++)&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;-&amp;gt; fails suddently when one day someone includes a.h into b.c, for example by changing the unrelated c.h header.&lt;BR /&gt;Worse than this case, defines can also intruduce silently wrong behaviour.&lt;BR /&gt;- many common traps. Just one of them:&lt;BR /&gt;#define TWO 1+1&lt;BR /&gt;#define FOUR TWO*TWO&lt;BR /&gt;-&amp;gt; value of FOUR is 3 (or something else, depends on context)&lt;BR /&gt;- can be hard to debug as debugger cannot know them.&lt;BR /&gt;- no explicit types, dangerous, complex rules&lt;BR /&gt;#define FOURTY_THOUSAND (20000 + 20000)&lt;BR /&gt;#if FOURTY_THOUSAND != 40000&lt;BR /&gt;#error // no error&lt;BR /&gt;#endif&lt;BR /&gt;void Error(void);&lt;BR /&gt;void test(void) {&lt;BR /&gt;if (FOURTY_THOUSAND != 40000) {&lt;BR /&gt;Error(); // ERROR is called!!!&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;Good&lt;BR /&gt;- no memory is ever allocated&lt;BR /&gt;- it works when done right&lt;BR /&gt;- can be used in macro expressions.&lt;BR /&gt;- can be used in type expressions&lt;BR /&gt;&lt;BR /&gt;- enums&lt;BR /&gt;&lt;BR /&gt;Bad&lt;BR /&gt;- no explicit types (but still a lot cleaner than for defines)&lt;BR /&gt;- cannot be used in macro expressions.&lt;BR /&gt;- ugly syntax, introduces a new type to define a value.&lt;BR /&gt;&lt;BR /&gt;Good&lt;BR /&gt;- follows scoping rules.&lt;BR /&gt;- can be used in type expressions&lt;BR /&gt;- no memory is ever allocated&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;- const int&lt;BR /&gt;&lt;BR /&gt;Bad&lt;BR /&gt;- cannot be used in macro expressions.&lt;BR /&gt;- cannot be used in type expressions (allowed in C++, not allowed in C!)&lt;BR /&gt;- no memory is usually allocated, but possible (adr taken, switched off optimization,...)&lt;BR /&gt;- can cause problems when put in header files (in C), not allowed to define same constant twice.&lt;BR /&gt;- With extern const int: no optimization possible.&lt;BR /&gt;&lt;BR /&gt;Good&lt;BR /&gt;- explict type, no surprises&lt;BR /&gt;- follows scoping rules&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;So in C, I'm usually using enums for simple numbers. Constants can be used in C++ or in C implementation files.&lt;BR /&gt;Using defines is advanced and dangerous, works when done right, but only then.&lt;BR /&gt;&lt;BR /&gt;Daniel&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 28 Jul 2006 20:52:36 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Classic-Legacy-CodeWarrior/define-verse-constant-declarations/m-p/124850#M51</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2006-07-28T20:52:36Z</dc:date>
    </item>
  </channel>
</rss>

