300 Chapter 13 The Preprocessor
defined as 1 , the preceding statement has the effect of assigning 1 to gameOver.The pre-
processor statement
#define NO 0
defines the name NOand makes its subsequent use in the program equivalent to specify-
ing the value 0 .Therefore, the statement
gameOver = NO;
assigns the value of NOto gameOver, and the statement
if ( gameOver == NO )
...
compares the value of gameOveragainst the defined value of NO.Just about the only
place that you cannotuse a defined name is inside a character string; so the statement
char *charPtr = "YES";
setscharPtrpointing to the string "YES"and not to the string "1".
A defined name is nota variable.Therefore, you cannot assign a value to it, unless the
result of substituting the defined value is in fact a variable.Whenever a defined name is
used in a program, whatever appears to the right of the defined name in the #define
statement gets automatically substituted into the program by the preprocessor. It’s analo-
gous to doing a search and replace with a text editor; in this case, the preprocessor
replaces all occurrences of the defined name with its associated text.
Notice that the #definestatement has a special syntax:There is no equal sign used to
assign the value 1 to YES. Furthermore, a semicolon does notappear at the end of the
statement. Soon, you will understand why this special syntax exists. But first, take a look
at a small program that uses the YESand NOdefines as previously illustrated.The function
isEvenin Program 13.1 simply returns YESif its argument is even and NOif its argument
is odd.Program 13.1 Introducing the #defineStatement
#include <stdio.h>#define YES 1
#define NO 0// Function to determine if an integer is evenint isEven (int number)
{
int answer;if ( number % 2 == 0 )
answer = YES;