Some help understanding #define with C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2004
Posts: 1
Reputation: JesterDev is an unknown quantity at this point 
Solved Threads: 0
JesterDev JesterDev is offline Offline
Newbie Poster

Some help understanding #define with C

 
0
  #1
Feb 16th, 2004
I'm learning C mostly for linux development, but right now I am working through a few general books to get me started. But I am having some problems understanding #define versus declaring a variable directly(?). I know that #define declares a constant, but for example normally I would declare a variable as a specified type like int Celsius; and then later could assign it a value. But with #define as it's written in the book I'm reading I wouldn't declare the variable as anything specific:

  1. #define CELSIUS 20

So later in code it automatically gets converted to a int right? For example:

  1.  
  2. int celsius;
  3.  
  4. for (CELSIUS = LOWER; CELSIUS <= UPPER; CELSIUS = CELSIUS + STEP)
  5. ...

Seems to me this could cause problems not only on future readability, but wouldn't it also lead to bad programming habits? Maybe I've taken all this out of context. Either way, can someone please explain to me what exactly is the difference?
Last edited by JesterDev; Feb 16th, 2004 at 7:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member

Re: Some help understanding #define with C

 
0
  #2
Feb 18th, 2004
Using #define in this way, CELSIUS is a symbolic constant. Everywhere that CELSIUS appears in this source file it is replaced with 20, simple text substitution.

So the following line:

for (CELSIUS = LOWER; CELSIUS <= UPPER; CELSIUS = CELSIUS + STEP)

becomes

for (20 = LOWER; 20 <= UPPER; 20 = 20 + STEP)

As you can see, this doesn't make sense in this context as 20 will be interpreted during compilation (after the text substitution has taken place) as an integer literal, not as a variable. You can't assign a value to an integer literal.

A better example might be to declare a variable named Celsius as an int value, then use symbolic constants for the (unchanging) values of UPPER, LOWER and STEP. For example:

#define LOWER 0
#define UPPER 100
#define STEP 2

int Celsius;
for(Celsius = LOWER; Celsius <= UPPER; Celsius += STEP)
......
......

This would be particularly useful when the symbolic constants LOWER, UPPER and STEP appear in several places in your file because:

- if the value does change for some reason you only have to change it in one place, e.g. your #define.

- it gives the value a meaningful name that conveys its purpose to the reader.

Does that help?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC