Pages: [1]   Go Down
Author Topic: Constant vs. #define Macro  (Read 387 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
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
Shannon Member
****
Karma: 223
Posts: 12630
-
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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
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: 207
Posts: 12921
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
Jump to: