The preprocessor is executed at translation phase 4, before the compilation. The result of preprocessing is a single file which is then passed to the actual compiler.
The preprocessing directives control the behavior of the preprocessor. Each directive occupies one line and has the following format:
#
character. #warning
which emits a user-defined message during compilation) (until C++23), or The module and import directives are also preprocessing directives. | (since C++20) |
Preprocessing directives must not come from macro expansion.
#define EMPTY EMPTY # include <file.h> // not a preprocessing directive
The preprocessor has the source file translation capabilities:
#if
, #ifdef
, #ifndef
, #else
, #elif
, #elifdef
, #elifndef
(since C++23), and #endif
). #define
and #undef
, and operators #
and ##
). #include
and checked with __has_include
(since C++17)). #error
or #warning
respectively (since C++23)). The following aspects of the preprocessor can be controlled:
#pragma
and operator _Pragma
(since C++11)). In addition, some compilers support (to varying degrees) the operator __pragma
as a non-standard extension. #line
). The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 2001 | C++98 | the behavior of using non-standard-defined directives was not clear | made conditionally-supported |
C++ documentation for Predefined Macro Symbols | |
C++ documentation for Macro Symbol Index | |
C documentation for preprocessor |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/preprocessor