static_assert(3)
π static_assert(3) π
okay bestie so this is a page from the Library Functions Manual β it's static_assert, and it's about to be your new fave compile-time bestie. no cap.
β¨ NAME β¨
static_assert, _Static_assert β fail compilation if assertion is false
like. it literally stops the build. on purpose. iconic behavior honestly.
π LIBRARY π
Standard C library (libc)
π SYNOPSIS π
#include <assert.h>
void static_assert(bool constant-expression, const char *msg);
/* Since C23: */
void static_assert(bool constant-expression);
π¦ DESCRIPTION π¦
so this macro is basically assert(3)'s glow-up β same energy, but it works at compile time. if the input is false (i.e., it compares equal to zero), you get a compilation error, and the message is optional. π
and if the input is nonzero? literally nothing happens. no code is emitted. the compiler just vibes. β¨
ngl, msg has to be a string literal. like, that's the rules. but since C23, that second argument is optional, so you can just skip it. π«Ά
also there's a keyword, _Static_assert(), that behaves identically, and the way that it works without including <assert.h>β¦ chef's kiss. you don't even need the header. main character energy fr fr.
π RETURN VALUE π
No value is returned. like, at all. zero. it's not that kind of macro, bestie. π
π STANDARDS π
C23.
β HISTORY β
C11.
back in C11, the second argument (msg) was mandatory β you had to give it one. but since C23, it can be omitted. glow-up confirmed. π
πΈ EXAMPLES πΈ
okay so quick tea: static_assert() can't be used in some places, like for example at global scope. π so if you want it there, you can write a macro called must_be() in terms of static_assert(). the program below uses that macro to get the size of an array safely. obsess.
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* This macro behaves like static_assert(), failing to
* compile if its argument is not true. However, it always
* returns 0, which allows using it everywhere an expression
* can be used.
*/
#define must_be(e) \
( \
0 * (int) sizeof( \
struct { \
static_assert(e); \
int ISO_C_forbids_a_struct_with_no_members; \
} \
) \
)
#define is_same_type(a, b) \
__builtin_types_compatible_p(typeof(a), typeof(b))
#define is_array(arr) (!is_same_type((arr), &*(arr)))
#define must_be_array(arr) must_be(is_array(arr))
#define sizeof_array(arr) (sizeof(arr) + must_be_array(arr))
#define NITEMS(arr) (sizeof((arr)) / sizeof((arr)[0]) \
+ must_be_array(arr))
int foo[10];
int8_t bar[sizeof_array(foo)];
int
main(void)
{
for (size_t i = 0; i < NITEMS(foo); i++) {
foo[i] = i;
}
memcpy(bar, foo, sizeof_array(bar));
for (size_t i = 0; i < NITEMS(bar); i++) {
printf("%d,", bar[i]);
}
exit(EXIT_SUCCESS);
}
π SEE ALSO π
and that's the whole page bestie!! π written up in Linux man-pages 6.20, dated 2026-04-20 β that's the source and version and date, so now you know exactly which era of the docs you're reading from. slay, go assert something at compile time, ily!! π§π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms