954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

#define or const?

should i use #define or const in declaring constant variables?? which one is better? when should i use #define or const?

for example when i declare a hexadecimal (0x01), should i use #define or const?

odee
Light Poster
27 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

should i use #define or const in declaring constant variables?? which one is better? when should i use #define or const?

for example when i declare a hexadecimal (0x01), should i use #define or const?

It depends.

A constant variable will take up space, and finding its value may take more time. A#define will, as per the example you have shown, simply be a text replacement of the value 1 in source code. That is, it is directly encoded into the executable.

But with a const, you get type checking. This may be an advantage to prefering it.

For array sizes, I prefer neither. Using sizeof array / sizeof *array will get you the number of elements when array is in scope, and where it isn't it IMO should be passed as a parameter.

For bit masks, I would use a #define, if not a macro like this.

#define BIT(x)  (1 << (x))


For data types other than integers, it too depends -- but I generally try to minimize the number of#defines in my code.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

hey thanks...

odee
Light Poster
27 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You