Namespaces
Variants
Views
Actions

Difference between revisions of "c/preprocessor"

From cppreference.com
< c
 
(18 intermediate revisions by 8 users not shown)
Line 1: Line 1:
 
{{title|Preprocessor}}
 
{{title|Preprocessor}}
 
{{c/preprocessor/navbar}}
 
{{c/preprocessor/navbar}}
The preprocessor is executed at [[c/language/translation_phases|translation phase 4]], before the compilation. The result of preprocessing is single file which is then passed to the actual compiler.
+
The preprocessor is executed at [[c/language/translation_phases|translation phase 4]], before the compilation. The result of preprocessing is a single file which is then passed to the actual compiler.
  
 
===Directives===
 
===Directives===
 
 
The preprocessing directives control the behavior of the preprocessor. Each directive occupies one line and has the following format:
 
The preprocessing directives control the behavior of the preprocessor. Each directive occupies one line and has the following format:
  
 
* {{tt|#}} character
 
* {{tt|#}} character
* preprocessing instruction (one of {{tt|define}}, {{tt|undef}}, {{tt|include}}, {{tt|if}}, {{tt|ifdef}}, {{tt|ifndef}}, {{tt|else}}, {{tt|elif}}, {{tt|endif}}, {{tt|line}}, {{tt|error}}, {{tt|warning}}, {{tt|pragma}}) <ref>These are the directives defined by the standard. The standard does not define behavior for other directives: they might be ignored, have some useful meaning, or make the program ill-formed. Even if otherwise ignored, they are removed from the source code when the preprocessor is done.</ref>
+
* preprocessing instruction (one of {{tt|define}}, {{tt|undef}}, {{tt|include}}, {{tt|if}}, {{tt|ifdef}}, {{tt|ifndef}}, {{tt|else}}, {{tt|elif}}{{rev inl|since=c23|, {{tt|elifdef}}, {{tt|elifndef}}}}, {{tt|endif}}, {{tt|line}}{{rev inl|since=c23|, {{tt|embed}}}}, {{tt|error}}{{rev inl|since=c23|, {{tt|warning}}}}, {{tt|pragma}}) <ref>These are the directives defined by the standard. The standard does not define behavior for other directives: they might be ignored, have some useful meaning, or make the program ill-formed. Even if otherwise ignored, they are removed from the source code when the preprocessor is done. {{rev inl|until=c23|A common non-standard extension is the directive {{tt|#warning}} which emits a user-defined message during compilation.}}</ref>
 
* arguments (depends on the instruction)
 
* arguments (depends on the instruction)
 
* line break
 
* line break
Line 15: Line 14:
  
 
===Capabilities===
 
===Capabilities===
 
 
The preprocessor has the source file translation capabilities:
 
The preprocessor has the source file translation capabilities:
  
* '''{{rl | conditional | conditionally}}''' compile of parts of source file (controlled by directive {{tt|#if}}, {{tt|#ifdef}}, {{tt|#ifndef}}, {{tt|#else}}, {{tt|#elif}} and {{tt|#endif}}).
+
* '''{{rl | conditional | conditionally}}''' compile of parts of source file (controlled by directive {{tt|#if}}, {{tt|#ifdef}}, {{tt|#ifndef}}, {{tt|#else}}, {{tt|#elif}}{{rev inl|since=c23|, {{tt|#elifdef}}, {{tt|#elifndef}}}} and {{tt|#endif}}).
 
* '''{{rl | replace}}''' text macros while possibly concatenating or quoting identifiers (controlled by directives {{tt|#define}} and {{tt|#undef}}, and operators {{tt|#}} and {{tt|##}})
 
* '''{{rl | replace}}''' text macros while possibly concatenating or quoting identifiers (controlled by directives {{tt|#define}} and {{tt|#undef}}, and operators {{tt|#}} and {{tt|##}})
* '''{{rl|include}}''' other files (controlled by directive {{tt|#include}})
+
* '''{{rl|include}}''' other files (controlled by directive {{tt|#include}} {{rev inl|since=c23|and checked with {{c|__has_include}}}})
* cause a '''{{rl|warning}}''' (controlled by directive {{tt|#warning}})
+
* cause an '''{{rl|error}}''' {{rev inl|since=c23|or '''{{rl|error|warning}}'''}} (controlled by directive {{tt|#error}}{{rev inl|since=c23| or {{tt|#warning}} respectively}})
* cause an '''{{rl|error}}''' (controlled by directive {{tt|#error}})
+
  
 
The following aspects of the preprocessor can be controlled:
 
The following aspects of the preprocessor can be controlled:
* '''{{rl|impl | implementation defined}}''' behavior (controlled by directives {{tt|#pragma}})
+
* '''{{rl|impl | implementation defined}}''' behavior (controlled by directive {{tt|#pragma}}{{rev inl|since=c99| and operator {{tt|_Pragma}}}})
 
* '''{{rl|line | file name and line information}}''' available to the preprocessor (controlled by directives {{tt|#line}})
 
* '''{{rl|line | file name and line information}}''' available to the preprocessor (controlled by directives {{tt|#line}})
  
 
===Footnotes===
 
===Footnotes===
 
<references/>
 
<references/>
 +
 +
===References===
 +
{{ref std c17}}
 +
{{ref std | section=6.10 | title=Preprocessing directives | p=117-129}}
 +
{{ref std end}}
 +
{{ref std c11}}
 +
{{ref std | section=6.10 | title=Preprocessing directives | p=160-178}}
 +
{{ref std end}}
 +
{{ref std c99}}
 +
{{ref std | section=6.10 | title=Preprocessing directives | p=145-162}}
 +
{{ref std end}}
 +
{{ref std c89}}
 +
{{ref std | section=3.8  | title=PREPROCESSING DIRECTIVES}}
 +
{{ref std end}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc see c|c/preprocessor/replace#Predefined macros|Predefined Macro Symbols|nomono=true}}
 +
{{dsc see c|c/symbol index/macro|Macro Symbol Index|nomono=true}}
 +
{{dsc see cpp|cpp/preprocessor|Preprocessor|nomono=true}}
 +
{{dsc end}}
 +
 +
{{langlinks|ar|cs|de|es|fr|it|ja|ko|pl|pt|ru|tr|zh}}

Latest revision as of 14:31, 22 June 2023

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.

Contents

[edit] Directives

The preprocessing directives control the behavior of the preprocessor. Each directive occupies one line and has the following format:

  • # character
  • preprocessing instruction (one of define, undef, include, if, ifdef, ifndef, else, elif, elifdef, elifndef(since C23), endif, line, embed(since C23), error, warning(since C23), pragma) [1]
  • arguments (depends on the instruction)
  • line break

The null directive (# followed by a line break) is allowed and has no effect.

[edit] Capabilities

The preprocessor has the source file translation capabilities:

  • conditionally compile of parts of source file (controlled by directive #if, #ifdef, #ifndef, #else, #elif, #elifdef, #elifndef(since C23) and #endif).
  • replace text macros while possibly concatenating or quoting identifiers (controlled by directives #define and #undef, and operators # and ##)
  • include other files (controlled by directive #include and checked with __has_include(since C23))
  • cause an error or warning(since C23) (controlled by directive #error or #warning respectively(since C23))

The following aspects of the preprocessor can be controlled:

[edit] Footnotes

  1. These are the directives defined by the standard. The standard does not define behavior for other directives: they might be ignored, have some useful meaning, or make the program ill-formed. Even if otherwise ignored, they are removed from the source code when the preprocessor is done. A common non-standard extension is the directive #warning which emits a user-defined message during compilation.(until C23)

[edit] References

  • C17 standard (ISO/IEC 9899:2018):
  • 6.10 Preprocessing directives (p: 117-129)
  • C11 standard (ISO/IEC 9899:2011):
  • 6.10 Preprocessing directives (p: 160-178)
  • C99 standard (ISO/IEC 9899:1999):
  • 6.10 Preprocessing directives (p: 145-162)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 3.8 PREPROCESSING DIRECTIVES

[edit] See also

C documentation for Predefined Macro Symbols
C documentation for Macro Symbol Index
C++ documentation for Preprocessor