•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 425,983 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,638 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 5309 | Replies: 2
![]() |
•
•
•
•
Originally Posted by odee
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?
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))
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- precompile directives and const (C++)
- first cannot conver std::string to const char, now runtime error! (C++)
- help my first game! (C++)
- random matrix generation (C)
- A question about 'const' (C++)
Other Threads in the C Forum
- Previous Thread: sound file switching
- Next Thread: needed recursion help



Linear Mode