| | |
Some help understanding #define with C
![]() |
•
•
Join Date: Feb 2004
Posts: 1
Reputation:
Solved Threads: 0
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:
So later in code it automatically gets converted to a int right? For example:
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?
C Syntax (Toggle Plain Text)
#define CELSIUS 20
So later in code it automatically gets converted to a int right? For example:
C Syntax (Toggle Plain Text)
int celsius; for (CELSIUS = LOWER; CELSIUS <= UPPER; CELSIUS = CELSIUS + STEP) ...
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.
•
•
Join Date: Feb 2003
Posts: 129
Reputation:
Solved Threads: 1
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?
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?
![]() |
Similar Threads
- Functional #define (C)
- Send data on a serial port (C++)
- Can't Download Movies (OS X)
- Define x86-64 vs i386 (Getting Started and Choosing a Distro)
- Need Help Understanding Subject in C++ (C++)
- writing a class without the class declaration? (C++)
- Difficulty understanding Object Orientated File Streaming (C++)
Other Threads in the C Forum
- Previous Thread: Help with compression program
- Next Thread: MFC diagram maker type thing
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h





