User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Dec 2004
Location: Ph
Posts: 27
Reputation: odee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
odee odee is offline Offline
Light Poster

#define or const?

  #1  
Feb 7th, 2005
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,623
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: #define or const?

  #2  
Feb 7th, 2005
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?
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.
Reply With Quote  
Join Date: Dec 2004
Location: Ph
Posts: 27
Reputation: odee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
odee odee is offline Offline
Light Poster

Re: #define or const?

  #3  
Feb 8th, 2005
hey thanks...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 11:39 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC