The C preprocessor or cpp is the macro preprocessor for the C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control.
In many C implementations, it is a separate program invoked by the compiler as the first part of translation.
The language of preprocessor directives is only weakly related to the grammar of C, and so is sometimes used to process other kinds of text files.
Video C preprocessor
Phases
Preprocessing is defined by the first four (of eight) phases of translation specified in the C Standard.
- Trigraph replacement: The preprocessor replaces trigraph sequences with the characters they represent.
- Line splicing: Physical source lines that are continued with escaped newline sequences are spliced to form logical lines.
- Tokenization: The preprocessor breaks the result into preprocessing tokens and whitespace. It replaces comments with whitespace.
- Macro expansion and directive handling: Preprocessing directive lines, including file inclusion and conditional compilation, are executed. The preprocessor simultaneously expands macros and, in the 1999 version of the C standard, handles
_Pragma
operators.
Including files
One of the most common uses of the preprocessor is to include another file:
The preprocessor replaces the line #include <stdio.h>
with the text of the file 'stdio.h', which declares the printf()
function among other things.
This can also be written using double quotes, e.g. #include "stdio.h"
. If the filename is enclosed within angle brackets, the file is searched for in the standard compiler include paths. If the filename is enclosed within double quotes, the search path is expanded to include the current source directory. C compilers and programming environments all have a facility which allows the programmer to define where include files can be found. This can be introduced through a command line flag, which can be parameterized using a makefile, so that a different set of include files can be swapped in for different operating systems, for instance.
By convention, include files are given a .h extension, and files not included by others are given a .c extension. However, there is no requirement that this be observed. Files with a .def extension may denote files designed to be included multiple times, each time expanding the same repetitive content; #include "icon.xbm"
is likely to refer to an XBM image file (which is at the same time a C source file).
#include
often compels the use of #include
guards or #pragma once
to prevent double inclusion.
Conditional compilation
The if-else directives #if
, #ifdef
, #ifndef
, #else
, #elif
and