# Preprocessor

> C++ Atlas: `cppatlas.com/w/c/preprocessor` | Source: https://en.cppreference.com/w/c/preprocessor | Domain: Language | Standards: C99, C23

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.

## Official Specification Summary (cppreference)

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.

### 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.

### 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:

- **implementation defined** behavior (controlled by directive `#pragma` and operator `_Pragma` (since C99))
- **file name and line information** available to the preprocessor (controlled by directives `#line`)

### 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)

### Example

> Note: This section is incomplete Reason: no example

### References

- C23 standard (ISO/IEC 9899:2024):

  - 6.10 Preprocessing directives (p: TBD)

- 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

### See also

- [C documentation for Predefined Macro Symbols](https://en.cppreference.com/w/c/preprocessor/replace#Predefined_macros)
- [C documentation for Macro Symbol Index](https://en.cppreference.com/w/c/symbol_index/macro)
- [C++ documentation for Preprocessor](https://en.cppreference.com/w/cpp/preprocessor)

---
Attribution: content from https://en.cppreference.com/w/c/preprocessor (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.