c / expert
Snippet
Compile-Time Constraints with _Static_assert
Introduced in C11, _Static_assert allows checking conditions at compile time rather than runtime. If the expression is false, the compiler generates an error with the provided string, ensuring that architectural or configuration requirements are met before the binary is even built.
snippet.c
1
2
3
4
5
6
7
8
9
#include <limits.h>#include <stdint.h>_Static_assert(sizeof(void*) == 8, "64-bit architecture required");_Static_assert(CHAR_BIT == 8, "Non-standard byte size not supported");int main() {return 0;}
Breakdown
1
_Static_assert(expr, msg)
Evaluates expr at compile time; halts compilation with msg if expr is zero.