C Reference ยท ctype.h, math.h and others
C stdalign.h and stdnoreturn.h
Reference for the legacy headers stdalign.h and stdnoreturn.h: the alignas, alignof and noreturn macros of C11, and why C23 made them keywords and [[noreturn]].
C11 introduced two language features with ugly names: the keywords _Alignas and _Alignof for alignment control, and _Noreturn for functions that never return. The new names started with an underscore and a capital letter so they could not collide with existing programs. To let programmers write the nicer spellings anyway, the standard added two tiny headers that only define macros:
<stdalign.h>definesalignasas_Alignasandalignofas_Alignof.<stdnoreturn.h>definesnoreturnas_Noreturn.
In C23 these macros are no longer needed. alignas, alignof, bool, true, false and thread_local became real keywords, and _Noreturn was superseded by the attribute [[noreturn]]. The headers remain for compatibility, and <stdnoreturn.h> is marked obsolescent. New code in C23 mode includes neither.
#include <stdalign.h> /* only needed before C23 */ alignas(16) char buffer[64]; size_t a = alignof(double);
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
#include <stdalign.h> alignas(constant-expression) object-declaration /* _Alignas */ alignas(type-name) object-declaration alignof(type-name) /* _Alignof: a size_t constant */ #define __alignas_is_defined 1 #define __alignof_is_defined 1 #include <stdnoreturn.h> noreturn function-declaration /* _Noreturn */ [[noreturn]] function-declaration /* C23 attribute, no header needed */
| Name | Description |
|---|---|
alignas(N) |
Requires the object to be aligned to N bytes, where N is zero or a valid alignment (a power of two). Cannot make alignment weaker than the type's natural one. |
alignas(type) |
Same as alignas(alignof(type)). |
alignof(type) |
The alignment in bytes the type needs; a size_t constant. |
noreturn |
Promises that the function never returns to its caller (it exits, aborts, loops forever or jumps away). |
Returns: the alignment macros are compile-time constants; noreturn generates no code.
Errors / undefined behavior: if a function marked noreturn does return, the behavior is undefined. An alignas stricter than the implementation supports (an over-aligned type) is implementation-defined, and may be rejected. Using alignof on a function type or incomplete type is a constraint violation.
Usage notes
- Prefer the C23 keywords. In C23 mode, drop
<stdalign.h>and<stdnoreturn.h>. GCC 15 acceptsalignas,alignofand[[noreturn]]with no include. The headers are still fine and keep working if you leave them in. - Use
<stdalign.h>in C11/C17. Before C23,alignasandalignofare only available through the header (or by writing_Alignasand_Alignofdirectly). alignoftakes a type.alignof(int[4])is 4 even if one particular object was declaredalignas(16). GCC also accepts an expression as an extension (and then reports the object's alignment), but portable code should check the address with(uintptr_t)p % 16.- Cache lines and SIMD. Typical uses of
alignas:alignas(64)to place hot data on its own cache line and avoid false sharing between threads, andalignas(16)oralignas(32)for data that SIMD instructions load. - Dynamic memory.
mallocreturns memory aligned formax_align_t. For stricter alignment usealigned_alloc;alignasapplies only to objects with static, thread or automatic storage and to struct members. noreturnhelps the compiler. It removes false warnings such as "control reaches end of non-void function" after calling it, and lets the optimizer drop unreachable code.exit,abort,longjmpandquick_exitare declared this way.- Standard version: both headers are C11. C23 makes
alignasandalignofkeywords (the headers then only define the two_is_definedmacros) and obsoletes<stdnoreturn.h>in favor of[[noreturn]].