On this page
_Static_assert
Prev: _Thread_local | Up: builtins | Next: _Noreturn
Rel: static_assert
Ref: cppreference — _Static_assert
Tags: #public #prog #c #builtins #tier/core
Availability
Built-in (Declaration (C11; static_assert in C23)) — requires no #include header.
Syntax & Overview
_Static_assert(const_expr, "message");
Fails compilation immediately if const_expr evaluates to 0 at compile time.
Notes & Pitfalls
- Compile-time invariant check (C11;
static_assertin C23): Zero runtime cost—great for verifying struct sizes, ABI assumptions, andCHAR_BIT == 8.
Example
#include <stdint.h>
struct Header { uint32_t magic; uint32_t len; };
_Static_assert(sizeof(struct Header) == 8, "Header must be 8 bytes");
Linked from
Related notes
- tuts/c/builtins/_Noreturn Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Thread_local Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins Linked here; links to this page; shared tags: #builtins, #c, #prog
- tuts/c/stdlib/assert/static_assert Linked here; links to this page; shared tags: #c, #prog, #tier
- tuts/c/builtins/__FILE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__func__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__LINE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__STDC_VERSION__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Alignof shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Atomic shared tags: #builtins, #c, #prog, #tier