Go Down

Topic: Constant vs. #define Macro (Read 457 times) previous topic - next topic

NJavrGuy

Hey everyone,

I know nowadays in C++ programming, using #define macros to define a constant is generally frowned upon, and the strong preference is to use constants.

Is there any reason in an Arduino library why a #define Macro may be better, faster, or more efficient than a constant?

If it matters, I am talking about unsigned short ints, or short ints.

Thanks.

PeterH


Is there any reason in an Arduino library why a #define Macro may be better, faster, or more efficient than a constant?


None that I can see, except when the code was written by somebody who has a personal preference for using #defines because that's what they are used to using.
I only provide help via the forum - please do not contact me for private consultancy.

Krodal

A "#define" could be overwritten without error.
A "const int" can not be overwritten with a different value.
And the compiled sketch has the same size.

Coding Badly

Is there any reason in an Arduino library why a #define Macro may be better,


See above plus a macro can be used in conditional compilation (#if #ifdef).

Quote
faster,


No.

Quote
or more efficient than a constant?


No.

Quote
If it matters, I am talking about unsigned short ints, or short ints.


It does not.


const is generally a better choice because it includes type.

Go Up