Programming in C

(Barry) #1

13 The Preprocessor


13 The Preprocessor


THIS CHAPTER DESCRIBES YET ANOTHER UNIQUE FEATUREof the C language that is
not found in many other higher-level programming languages.The C preprocessor pro-
vides the tools that enable you to develop programs that are easier to develop, easier to
read, easier to modify, and easier to port to a different computer system.You can also use
the preprocessor to literally customize the C language to suit a particular programming
application or to satisfy your own programming style.
The preprocessor is a part of the C compilation process that recognizes special state-
ments that might be interspersed throughout a C program. As its name implies, the pre-
processor actually analyzes these statements beforeanalysis of the C program itself takes
place. Preprocessor statements are identified by the presence of a pound sign,#, which
must be the first nonspace character on the line. As you will see, preprocessor statements
have a syntax that is slightly different from that of normal C statements.You begin by
examining the #definestatement.


The #defineStatement


One of the primary uses of the #definestatement is to assign symbolic names to pro-
gram constants.The preprocessor statement


#define YES 1


defines the name YESand makes it equivalent to the value 1 .The name YEScan subse-
quently be used anywhere in the program where the constant 1 could be used.
Whenever this name appears, its defined value of 1 is automatically substituted into the
program by the preprocessor. For example, you might have the following C statement
that uses the defined name YES:


gameOver = YES;


This statement assigns the value of YESto gameOver.You don’t need to concern yourself
with the actual value that you defined for YES,but because you do know that it is

Free download pdf