Saturday, October 17, 2009

The #ifdef and #endif Directives

The #ifdef and #endif directives control whether a given group of statements is to be included as part of your
program.
The general form to use the #ifdef and #endif directives is
#ifdef macro_name
statement1
statement2
. . .
statementN
#endif
Here macro_name is any character string that can be defined by a #define directive. statement1, statement2, and
statementN are statements that are included in the program if macro_name has been defined. If macro_name has not
been defined, statement1, statement2, and statementN are skipped.
Because the statements under the control of the #ifdef directive are not enclosed in braces, the #endif directive must
be used to mark the end of the #ifdef block.
For instance, the #ifdef directive in the following code segment:
. . .
#ifdef DEBUG
printf("The contents of the string pointed to by str: %s\n", str);
#endif
. . .
indicates that if the macro name DEBUG is defined, the printf() function in the statement following the #ifdef
directive is included in the program. The compiler will compile the statement so that the contents of a string pointed
to by str can be printed out after the statement is executed.

No comments:

Post a Comment