Loading...
Pages: [1]   Go Down
Author Topic: Constant vs. #define Macro  (Read 110 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Tesla Member
***
Karma: 89
Posts: 6396
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 9
Posts: 1000
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 119
Posts: 10172
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Print
 
Jump to: