Preprocessor
The preprocessor is a program that edits C programs prior to compilation. It uses preprocessing directives which are commands that begin with a #
character. For example, the #include
directive is used to include header files in a C program. This directive tells the preprocessor to include the contents of the specified header file in the program.
Preprocessing Directives
Preprocessing directives fall into three categories:
- Macro definition.
#define
is used to define macros.#undef
is used to undefine macros. - File inclusion.
#include
causes the contents of a specified file to be included in the program. - Conditional compilation.
#if
,#ifdef
,#ifndef
,#elif
,#else
and#endif
are used to conditionally compile parts of a program.
Macro Definitions
We'll look at macro definitions first. A macro is a name that represents a sequence of characters. When the preprocessor encounters a macro name in a program, it replaces the name with the sequence of characters that the macro represents. This process is called macro expansion.
Simple Macros
Simple macros are defined using the #define
directive. The syntax is:
#define name replacement
The name
is the name of the macro and the replacement
is the sequence of characters that the macro represents. For example, the following macro defines the name PI
to represent the value 3.14159
:
#define PI 3.14159
#define PRINT printf("Hello, world!\n")
Notice the lack of a semicolon at the end of the PRINT
macro. In C, macros
defined using the #define
preprocessor directive do not require a semicolon
at the end. In fact, if you put a semicolon at the end of a macro definition,
the macro will include that semicolon when it's expanded, which might not be
what you want.
When the preprocessor encounters the name PI
in a program, it replaces it with the value 3.14159
. Consider the following program:
#include <stdio.h>
#define PI 3.14159
#define PRINT printf("Hello, world!\n")
int main(void) {
printf("PI = %f\n", PI);
PRINT;
return 0;
}
The preprocessor replaces the name PI
with the value 3.14159
before the program is compiled. The compiler sees the following program:
#include <stdio.h>
int main(void) {
printf("PI = %f\n", 3.14159);
printf("Hello, world!\n");
return 0;
}
Parameterized Macros
Parameterized macros are function-like macros. The syntax is:
#define name(parameters) replacement
There must be no whitespace between a parameterized macro name and the left parenthesis.
The name
is the name of the macro and the replacement
is the sequence of characters that the macro represents. The parameters
are the parameters of the macro. For example, the following macro defines the name MAX
to represent the maximum of two values:
#define MAX(a, b) ((a) > (b) ? (a) : (b))
When the preprocessor encounters the name MAX
in a program, it replaces it with the sequence of characters ((a) > (b) ? (a) : (b))
. Consider the following program:
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int main(void) {
printf("MAX(3, 5) = %d\n", MAX(3, 5)); // prints MAX(3, 5) = 5
return 0;
}
#
Operator
The content of this section is not yet ready. Check back in a few days.
##
Operator
The content of this section is not yet ready. Check back in a few days.
General Properties of Macros
The content of this section is not yet ready. Check back in a few days.
Parantheses in Macro Definitions
The content of this section is not yet ready. Check back in a few days.
Creating Longer Macros
The content of this section is not yet ready. Check back in a few days.
Predefined Macros
The content of this section is not yet ready. Check back in a few days.
Empty Macro Arguments
The content of this section is not yet ready. Check back in a few days.
Macros with Variable Number of Arguments
The content of this section is not yet ready. Check back in a few days.
__func__
Identifier
The content of this section is not yet ready. Check back in a few days.
Conditional Compilation
#if
and #endif
Directives
The content of this section is not yet ready. Check back in a few days.
defined
Operator
The content of this section is not yet ready. Check back in a few days.
#ifdef
and #ifndef
Directives
The content of this section is not yet ready. Check back in a few days.
#elif
and #else
Directives
The content of this section is not yet ready. Check back in a few days.
Miscalleanous Directives
#error
Directive
The content of this section is not yet ready. Check back in a few days.
#pragma
Directive
The content of this section is not yet ready. Check back in a few days.
_Pragma
Operator
The content of this section is not yet ready. Check back in a few days.