C Reference ยท Preprocessor
The #pragma directive and _Pragma operator
Reference for C #pragma and _Pragma: standard STDC pragmas, common compiler pragmas such as once, pack and GCC diagnostic, and how to use them in macros.
#pragma passes an instruction to the compiler that is not part of the language proper. Its meaning is implementation-defined, and a compiler that does not recognize a pragma is allowed to ignore it (GCC warns with -Wunknown-pragmas, part of -Wall). Only the STDC family of pragmas is standardized.
_Pragma("text") is the operator form. Unlike the directive it is an expression-like token, so it can appear in a macro replacement, where a #pragma line cannot.
#pragma once #pragma GCC diagnostic ignored "-Wunused-parameter" _Pragma("GCC diagnostic push")
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
#pragma pp-tokens _Pragma ( string-literal ) #pragma STDC FP_CONTRACT on-off-switch #pragma STDC FENV_ACCESS on-off-switch #pragma STDC CX_LIMITED_RANGE on-off-switch
| Name | Description |
|---|---|
STDC FP_CONTRACT |
Allow or forbid fusing floating-point operations (for example into fused multiply-add). |
STDC FENV_ACCESS |
Tell the compiler the program reads or changes the floating-point environment. |
STDC CX_LIMITED_RANGE |
Allow simpler complex-number formulas. |
string-literal |
The pragma text for _Pragma; embedded quotes and backslashes must be escaped. |
Errors / undefined behavior: unknown pragmas are ignored (possibly with a warning). An ill-formed STDC pragma is a constraint violation.
Usage notes
- Portable parts: only the
STDCpragmas. Everything else (once,pack,GCC diagnostic,omp,region) belongs to a particular compiler or tool. #pragma onceworks on GCC, Clang and MSVC, but is not standard. Include guards are always portable.#pragma pack(n)changes struct padding for later struct definitions; it changes layout and can cause misaligned access. Prefer designing a layout over packing it.- Diagnostic pragmas (
GCC diagnostic push/ignored/pop) are scoped: alwayspopwhat youpush. Also accepted by Clang. - In macros use
_Pragma.#pragmacannot appear inside a macro replacement._Pragma("...")is processed after expansion, so it works. - C23 attributes (
[[maybe_unused]],[[deprecated]]) are the standard way to express many things pragmas were used for. See attributes. - Do not hide bugs. A silenced warning should come with a comment saying why.
Related
- Include guards, #error and #warning, attributes
- Tutorial: The preprocessor