Namespaces
Variants
Views
Actions

Difference between revisions of "c/preprocessor"

From cppreference.com
< c
m (see also C++)
(no #warning in C)
Line 8: Line 8:
  
 
* {{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}}, {{tt|endif}}, {{tt|line}}, {{tt|error}}, {{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. 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 21: Line 21:
 
* '''{{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}})
* cause a '''{{rl|warning}}''' (controlled by directive {{tt|#warning}})
 
 
* cause an '''{{rl|error}}''' (controlled by directive {{tt|#error}})
 
* cause an '''{{rl|error}}''' (controlled by directive {{tt|#error}})
  

Revision as of 06:37, 19 July 2014

The preprocessor is executed at translation phase 4, before the compilation. The result of preprocessing is single file which is then passed to the actual compiler.

Contents

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, endif, line, error, pragma) [1]
  • arguments (depends on the instruction)
  • line break

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

Capabilities

The preprocessor has the source file translation capabilities:

  • conditionally compile of parts of source file (controlled by directive #if, #ifdef, #ifndef, #else, #elif 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)
  • cause an error (controlled by directive #error)

The following aspects of the preprocessor can be controlled:

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.

See also

C++ documentation for preprocessor