NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |VERSIONS |STANDARDS |EXAMPLES |SEE ALSO |COLOPHON | |
static_assert(3) Library Functions Manualstatic_assert(3)static_assert, _Static_assert - fail compilation if assertion is false
Standard C library (libc)
#include <assert.h>void static_assert(boolconstant-expression, const char *msg); /* Since C23: */void static_assert(boolconstant-expression);
This macro is similar toassert(3), but it works at compile time, generating a compilation error (with an optional message) when the input is false (i.e., compares equal to zero). If the input is nonzero, no code is emitted.msg must be a string literal. Since C23, this argument is optional. There's a keyword,_Static_assert(), that behaves identically, and can be used without including<assert.h>.
No value is returned.
In C11, the second argument (msg) was mandatory; since C23, it can be omitted.
C11 and later.
static_assert() can't be used in some places, like for example at global scope. For that, a macromust_be() can be written in terms ofstatic_assert(). The following program uses the macro to get the size of an array safely. #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); }assert(3)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17static_assert(3)HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |