The C preprocessor modifies a source file before handing it over to the compiler, allowing conditional compilation with #ifdef, defining constants with #define, including header files with #include. This page lists the preprocessor directives, or commands to the preprocessor
#include
#include <header name>
The include directive instructs the preprocessor to paste the text of the given file into the current file. Generally, it is necessary to tell the preprocessor where to look for header files if they are not placed in the current directory or a standard system directory. This can be done either at compile time or as part of your compiler's project file. This feature is implementation-specific, so see the compilers page for more information.
Defining a constant
#define token [value]
When defining a constant, you may optionally elect not to provide a value for that constant. In this case, the token will be replaced with blank text, but will be "defined" for the purposes of #ifdef and ifndef. If a value is provided, the given token will be replaced literally with the remainder of the text on the line. You should be careful when using #define in this way; see this article on the c preprocessor for a list of gotchas.
For instance, here is a standard way of writing a macro to return the max of two values.
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Note that when writing macros there are a lot of small gotchas; you can read more about it here: the c preprocessor . To define a multiline macro, each line before the last should end with a \, which will result in a line continuation.
#include
#include <header name>
The include directive instructs the preprocessor to paste the text of the given file into the current file. Generally, it is necessary to tell the preprocessor where to look for header files if they are not placed in the current directory or a standard system directory. This can be done either at compile time or as part of your compiler's project file. This feature is implementation-specific, so see the compilers page for more information.
Defining a constant
#define token [value]
When defining a constant, you may optionally elect not to provide a value for that constant. In this case, the token will be replaced with blank text, but will be "defined" for the purposes of #ifdef and ifndef. If a value is provided, the given token will be replaced literally with the remainder of the text on the line. You should be careful when using #define in this way; see this article on the c preprocessor for a list of gotchas.
For instance, here is a standard way of writing a macro to return the max of two values.
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Note that when writing macros there are a lot of small gotchas; you can read more about it here: the c preprocessor . To define a multiline macro, each line before the last should end with a \, which will result in a line continuation.