Skip to content
Tutorials Center
My learning

Topics

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> defines alignas as _Alignas and alignof as _Alignof.
  • <stdnoreturn.h> defines noreturn as _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.

C
#include <stdalign.h>      /* only needed before C23 */

alignas(16) char buffer[64];
size_t a = alignof(double);

Try it

C alignas and alignof

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.

C noreturn functions

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

C
#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 accepts alignas, alignof and [[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, alignas and alignof are only available through the header (or by writing _Alignas and _Alignof directly).
  • alignof takes a type. alignof(int[4]) is 4 even if one particular object was declared alignas(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, and alignas(16) or alignas(32) for data that SIMD instructions load.
  • Dynamic memory. malloc returns memory aligned for max_align_t. For stricter alignment use aligned_alloc; alignas applies only to objects with static, thread or automatic storage and to struct members.
  • noreturn helps 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, longjmp and quick_exit are declared this way.
  • Standard version: both headers are C11. C23 makes alignas and alignof keywords (the headers then only define the two _is_defined macros) and obsoletes <stdnoreturn.h> in favor of [[noreturn]].