Static assert in C

How can compile-time static asserts be implemented in C (not C++), with particular emphasis on GCC?


  • C11 standard adds the _Static_assert keyword.

    This is implemented since gcc-4.6:

    _Static_assert (0, "assert1"); /* { dg-error "static assertion failed: \"assert1\"" } */

    The first slot needs to be an integral constant expression. The second slot is a constant string literal which can be long (_Static_assert(0, L"assertion of doom!")).

    I should note that this is also implemented in recent versions of clang.