The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every
occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).
The syntax for the #define directive is
#define macro_name macro_body
Here macro_name is an identifier that can contain letters, numerals, or underscores. macro_body may be a string or a
data item, which is used to substitute each macro_name found in the program.
As mentioned earlier, the operation to replace occurrences of macro_name with the value specified by macro_body is
known as macro substitution or macro expansion.
The value of the macro body specified by a #define directive can be any character string or number. For example, the
following definition associates STATE_NAME with the string "Texas" (including the quotation marks):
#define STATE_NAME "Texas"
Then, during preprocessing, all occurrences of STATE_NAME will be replaced by "Texas".
Likewise, the following statement tells the C preprocessor to replace SUM with the string (12 + 8):
#define SUM (12 + 8)
On the other hand, you can use the #undef directive to remove the definition of a macro name that has been
previously defined.
The syntax for the #undef directive is
#undef macro_name
Here macro_name is an identifier that has been previously defined by a #define directive.
The #undef directive "undefines" a macro name. For instance, the following segment of code:
#define STATE_NAME "Texas"
printf("I am moving out of %s.\n", STATE_NAME);
#undef STATE_NAME
defines the macro name STATE_NAME first, and uses the macro name in the printf() function; then it removes the
macro name.
Saturday, October 17, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment