Programming in C

(Barry) #1

306 Chapter 13 The Preprocessor


else if ( n > 0 ) /* left rotate */
{
bits = value >> (kIntSize - n);
result = value << n | bits;
}
else /* right rotate */
{
n = -n;
bits = value << (kIntSize - n) ;
result = value >> n | bits;
}

return result;
}

More Advanced Types of Definitions


A definition for a name can include more than a simple constant value. It can include an
expression, and, as you will see shortly, just about anything else!
The following defines the name TWO_PIas the product of 2.0and 3.141592654:
#define TWO_PI 2.0 * 3.141592654
You can subsequently use this defined name anywhere in a program where the expres-
sion 2.0 × 3.141592654would be valid. So you could have replaced the returnstate-
ment of the circumferencefunction from the previous program with the following
statement, for example:
return TWO_PI * r;
Whenever a defined name is encountered in a C program,everythingthat appears to the
right of the defined name in the #definestatement is literally substituted for the name
at that point in the program. So, when the C preprocessor encounters the name TWO_PI
in the returnstatement shown previously, it substitutes for this name whatever appeared
in the #definestatement for this name.Therefore,2.0 × 3.141592654is literally sub-
stituted by the preprocessor whenever the defined name TWO_PIoccurs in the program.
The fact that the preprocessor performs a literal text substitution whenever the
defined name occurs explains why you don’t usually want to end your #definestate-
ment with a semicolon. If you did, then the semicolon would also be substituted into the
program wherever the defined name appeared. If you had defined PIas
#define PI 3.141592654;
and then written
return 2.0 * PI * r;
Free download pdf