Chapter: Programming and Data Structures: C Preprocessor
Chapter: Programming and Data Structures: C Preprocessor
C Preprocessor
The C Preprocessor is not part of the compiler, but is a separate step in the
compilation process. In simplistic terms, a C Preprocessor is just a text substitution
tool and they instruct compiler to do required pre-processing before actual
compilation. We'll refer to the C Preprocessor as the CPP.
All preprocessor commands begin with a pound symbol (#). It must be the first
nonblank character, and for readability, a preprocessor directive should begin in
first column. Following section lists down all important preprocessor directives:
Directive Description
2. Preprocessors Examples
Analyze the following examples to understand various directives.
#define MAX_ARRAY_LENGTH 20
These directives tell the CPP to get stdio.h from System Libraries and add
the text to the current source file. The next line tells CPP to get myheader.h from
the local directory and add the content to the current source file.
This tells the CPP to undefine existing FILE_SIZE and define it as 42.
#ifndef MESSAGE
#ifdef DEBUG
#endif
This tells the CPP to do the process the statements enclosed if DEBUG is
defined. This is useful if you pass the -DDEBUG flag to gcc compiler at the time
of compilation. This will define DEBUG, so you can turn debugging on and off on
the fly during compilation.
3. Predefined Macros
ANSI C defines a number of macros. Although each one is available for your
use in programming, the predefined macros should not be directly modified.
Macro Description
__DATE__ The current date as a character literal in "MMM DD YYYY" format
__STDC__ Defined as 1 when the compiler complies with the ANSI standard.
#include <stdio.h>
main()
When the above code in a file test.c is compiled and executed, it produces the
following result:
File :test.c
Date :Jun 2 2012
Time :03:36:24
Line :8
ANSI :1
Preprocessor Operators
The C preprocessor offers following operators to help you in creating macros:
#define message_for(a, b) \
printf(#a " and " #b ": We love you!\n")
Stringize (#)
The stringize or number-sign operator ('#'), when used within a macro definition,
converts a macro parameter into a string constant. This operator may be used only
in a macro that has a specified argument or parameter list. For example:
#include <stdio.h>
#define message_for(a, b) \
int main(void)
}
When the above code is compiled and executed, it produces the following
result:
#include <stdio.h>
int main(void)
tokenpaster(34); return 0;
}
When the above code is compiled and executed, it produces the following
result:
token34 = 40
This example shows the concatenation of token##n into token34 and here we have
used both stringize and token-pasting.
int main(void)
{
When the above code is compiled and executed, it produces the following result:
Parameterized Macros
One of the powerful functions of the CPP is the ability to simulate functions
using parameterized macros. For example, we might have some code to square a
number as follows:
int square(int x)
{
return x * x;
}
Macros with arguments must be defined using the #define directive before they
can be used. The argument list is enclosed in parentheses and must immediately
follow the macro name. Spaces are not allowed between and macro name and open
parenthesis. For example:
#include <stdio.h>
When the above code is compiled and executed, it produces the following result: