LPCOpen: #define vs const usage

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPCOpen: #define vs const usage

768 Views
karthikvenkates
Contributor II

Hi,

In LPCOpen for LPC812, I could find two conventions being following while returning value from a function.

For example, Chip_Clock_GetIntOscRate() function returns "SYSCTL_IRC_FREQ" which is defined as 12000000 using #define as shown below:

pastedImage_2.png

pastedImage_1.png

But in function Chip_Clock_GetMainOscRate(), "OscRateIn" is returned where OscRateInn is a const variable with value 12000000, as shown below.

pastedImage_4.png

pastedImage_3.png

I understand the difference between #define and const.

#define - does not allocate memory. Just a text replacment. Cannot be passed as pointer in the function argument.

const - allocates dedicated memory in Flash and the value cannot be changed. Address of this can be used as argument of a function.

But my question is, why cant NXP could have simple used #define OscRateIn  (12000000) instead of const uint32_t OscRateIn = 12000000 ?

Labels (4)
3 Replies

570 Views
marcprager
Contributor III

Interestingly, nowhere you found the use of enum - which is the only C-language approach without 'namespace' conflict potential or memory wasting potential  .-)

Yes, if we have a good, optimizing compiler/linker, then the result is the same, for all approaches.

If someone offers me code with more than a handful of #defines I dismiss both the code and the person as a programmer.

0 Kudos

570 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hello Marc Prager,

   Some description from the website:

#define is a preprocessor directive. Things defined by #define are replaced by the preprocessor before compilation begins.

const variables are actual variables like other normal variable.

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.

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.

A #define is used as immediate constant or as a macro. Where as the constant is a variable whose value can not change.

Pointer can be declared to a constant, but not for #define.

#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 .

Wish it helps you!


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

570 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hello karthik venkatesh ,

    I think it is just the code write habit question.

    Maybe the lpcopen code is not finished by one engineer, some people like to use the define, some like constant.

    But they work for different file, so there has inconsistent problem.

    But I think this is not the problem, in the practical usage, you can use the method which you like.


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos