<?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>LPC MicrocontrollersのトピックRe: LPCOpen: #define vs const usage</title>
    <link>https://community.nxp.com/t5/LPC-Microcontrollers/LPCOpen-define-vs-const-usage/m-p/692945#M27932</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Marc Prager,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Some description from the website:&lt;/P&gt;&lt;P&gt;#define is a preprocessor directive. Things defined by #define are replaced by the preprocessor before compilation begins.&lt;/P&gt;&lt;P&gt;const variables are actual variables like other normal variable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The big advantage of const over #define is type checking. We can also have pointers to const variables, we can pass them around, typecast them and any other thing that can be done with a normal variable. One disadvantage that one could think of is extra space for variable which is immaterial due to optimizations done by compilers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In general const is a better option if we have a choice. There are situations when #define cannot be replaced by const. For example, #define can take parameters (See this for example). #define can also be used to replace some text in a program with another text.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A #define is used as immediate constant or as a macro. Where as the constant is a variable whose value can not change.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Pointer can be declared to a constant, but not for #define.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#define can not define externally, and there is no way to use it for availability to the linker. Where as the constant can be global .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Wish it helps you!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Have a great day,&lt;BR /&gt;Kerry&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 31 May 2017 01:57:08 GMT</pubDate>
    <dc:creator>kerryzhou</dc:creator>
    <dc:date>2017-05-31T01:57:08Z</dc:date>
    <item>
      <title>LPCOpen: #define vs const usage</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/LPCOpen-define-vs-const-usage/m-p/692942#M27929</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In LPCOpen for LPC812, I could find two conventions being following while returning value from a function.&lt;/P&gt;&lt;P&gt;For example, Chip_Clock_GetIntOscRate() function returns "SYSCTL_IRC_FREQ" which is defined as 12000000 using #define as shown below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="pastedImage_2.png"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/19560iBDB47E26702D1B12/image-size/large?v=v2&amp;amp;px=999" role="button" title="pastedImage_2.png" alt="pastedImage_2.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="pastedImage_1.png"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/19527iE03139844780A1AD/image-size/large?v=v2&amp;amp;px=999" role="button" title="pastedImage_1.png" alt="pastedImage_1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But in function Chip_Clock_GetMainOscRate(), "OscRateIn" is returned where OscRateInn is a const variable with value 12000000, as shown below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="pastedImage_4.png"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/19653i28EE06E9F6FFA434/image-size/large?v=v2&amp;amp;px=999" role="button" title="pastedImage_4.png" alt="pastedImage_4.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="pastedImage_3.png"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/19584i0968BCB7AD092760/image-size/large?v=v2&amp;amp;px=999" role="button" title="pastedImage_3.png" alt="pastedImage_3.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I understand the difference between #define and const.&lt;/P&gt;&lt;P&gt;#define - does not allocate memory. Just a text replacment. Cannot be passed as pointer in the function argument.&lt;/P&gt;&lt;P&gt;const - allocates dedicated memory in Flash and the value cannot be changed. Address of this can be used as argument of a function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But my question is, why cant NXP could have simple used #define OscRateIn&amp;nbsp; (12000000) instead of const uint32_t OscRateIn = 12000000 ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 May 2017 00:30:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/LPCOpen-define-vs-const-usage/m-p/692942#M27929</guid>
      <dc:creator>karthikvenkates</dc:creator>
      <dc:date>2017-05-25T00:30:27Z</dc:date>
    </item>
    <item>
      <title>Re: LPCOpen: #define vs const usage</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/LPCOpen-define-vs-const-usage/m-p/692943#M27930</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello &lt;A _jive_internal="true" class="" data-content-finding="Community" data-userid="283524" data-username="karthikvenkatesh" href="https://community.nxp.com/people/karthikvenkatesh"&gt;karthik venkatesh&lt;/A&gt; ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; I think it is just the code write habit question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Maybe the lpcopen code is not finished by one engineer, some people like to use the define, some like constant.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; But they work for different file, so there has inconsistent problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; But I think this is not the problem, in the practical usage, you can use the method which you like.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Have a great day,&lt;BR /&gt;Kerry&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 26 May 2017 08:46:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/LPCOpen-define-vs-const-usage/m-p/692943#M27930</guid>
      <dc:creator>kerryzhou</dc:creator>
      <dc:date>2017-05-26T08:46:08Z</dc:date>
    </item>
    <item>
      <title>Re: LPCOpen: #define vs const usage</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/LPCOpen-define-vs-const-usage/m-p/692944#M27931</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Interestingly, nowhere you found the use of enum - which is the only C-language approach without 'namespace' conflict potential or memory wasting potential&amp;nbsp; .-)&lt;/P&gt;&lt;P&gt;Yes, if we have a good, optimizing compiler/linker, then the result is the same, for all approaches.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If someone offers me code with more than a handful of #defines I dismiss both the code and the person as a programmer.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 30 May 2017 22:33:00 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/LPCOpen-define-vs-const-usage/m-p/692944#M27931</guid>
      <dc:creator>marcprager</dc:creator>
      <dc:date>2017-05-30T22:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: LPCOpen: #define vs const usage</title>
      <link>https://community.nxp.com/t5/LPC-Microcontrollers/LPCOpen-define-vs-const-usage/m-p/692945#M27932</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Marc Prager,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Some description from the website:&lt;/P&gt;&lt;P&gt;#define is a preprocessor directive. Things defined by #define are replaced by the preprocessor before compilation begins.&lt;/P&gt;&lt;P&gt;const variables are actual variables like other normal variable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The big advantage of const over #define is type checking. We can also have pointers to const variables, we can pass them around, typecast them and any other thing that can be done with a normal variable. One disadvantage that one could think of is extra space for variable which is immaterial due to optimizations done by compilers.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In general const is a better option if we have a choice. There are situations when #define cannot be replaced by const. For example, #define can take parameters (See this for example). #define can also be used to replace some text in a program with another text.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A #define is used as immediate constant or as a macro. Where as the constant is a variable whose value can not change.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Pointer can be declared to a constant, but not for #define.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#define can not define externally, and there is no way to use it for availability to the linker. Where as the constant can be global .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Wish it helps you!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Have a great day,&lt;BR /&gt;Kerry&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 31 May 2017 01:57:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPC-Microcontrollers/LPCOpen-define-vs-const-usage/m-p/692945#M27932</guid>
      <dc:creator>kerryzhou</dc:creator>
      <dc:date>2017-05-31T01:57:08Z</dc:date>
    </item>
  </channel>
</rss>

