|
| 1 | +--- |
| 2 | +title: "_Static_assert keyword and static_assert macro (C11)" |
| 3 | +description: "Describes the C11 _Static_assert keyword and the C11 static_assert macro." |
| 4 | +ms.date: "10/13/2020" |
| 5 | +f1_keywords: ["static_assert_c", "_Static_assert"] |
| 6 | +helpviewer_keywords: ["assertions [C], _Static_assert, static_assert"] |
| 7 | +--- |
| 8 | + |
| 9 | +# _Static_assert keyword and static_assert macro (C11) |
| 10 | + |
| 11 | +Tests an assertion at compile time. If the specified constant expression is **`false`**, the compiler displays the specified message and the compilation fails with error C2338; otherwise, there's no effect. New in C11. |
| 12 | + |
| 13 | +**`_Static_assert`** is a keyword introduced in C11. |
| 14 | +**`static_assert`** is a macro, introduced in C11, that maps to the **`_Static_assert`** keyword. |
| 15 | + |
| 16 | +## Syntax |
| 17 | + |
| 18 | +```C |
| 19 | +_Static_assert(constant-expression, string-literal); |
| 20 | +static_assert(constant-expression, string-literal); |
| 21 | +``` |
| 22 | +
|
| 23 | +### Parameters |
| 24 | +
|
| 25 | +*constant-expression*\ |
| 26 | +An integral constant expression that can be evaluated at compile time. If the expression is zero (false), displays the *string-literal* parameter and the compilation fails with an error. If the expression is nonzero (true), then there's no effect. |
| 27 | +
|
| 28 | +*string-literal*\ |
| 29 | +The message displayed if *constant-expression* evaluates to zero (false). The message must be made using the [base character set](../c-language/ascii-character-set.md) of the compiler. The characters can't be [multibyte or wide characters](../c-language/multibyte-and-wide-characters.md). |
| 30 | +
|
| 31 | +## Remarks |
| 32 | +
|
| 33 | +The **`_Static_assert`** keyword, and the **`static_assert`** macro, both test a software assertion at compile time. They can be used at global or function scope. |
| 34 | +
|
| 35 | +In contrast, the [assert macro and _assert and _wassert functions](../c-runtime-library/reference/assert-macro-assert-wassert.md) test a software assertion at runtime and incur a runtime cost. |
| 36 | +
|
| 37 | +**Microsoft-specific behavior** |
| 38 | +
|
| 39 | +In C, when you don't include `<assert.h>`, the Microsoft Visual C/C++ compiler treats **`static_assert`** as a keyword that maps to **`_Static_assert`**. Using **`static_assert`** is preferred because the same code will work in both C and C++. |
| 40 | +
|
| 41 | +## Example of a compile-time assert |
| 42 | +
|
| 43 | +In the following example, **`static_assert`** and **`_Static_assert`** are used to verify how many elements are in an enum and that integers are 32 bits wide. |
| 44 | +
|
| 45 | +```C |
| 46 | +// requires /std:c11 or higher |
| 47 | +#include <assert.h> |
| 48 | +
|
| 49 | +enum Items |
| 50 | +{ |
| 51 | + A, |
| 52 | + B, |
| 53 | + C, |
| 54 | + LENGTH |
| 55 | +}; |
| 56 | +
|
| 57 | +int main() |
| 58 | +{ |
| 59 | + // _Static_assert is a C11 keyword |
| 60 | + _Static_assert(LENGTH == 3, "Expected Items enum to have three elements"); |
| 61 | +
|
| 62 | + // Preferred: static_assert maps to _Static_Assert and is compatible with C++ |
| 63 | + static_assert(sizeof(int) == 4, "Expecting 32 bit integers"); |
| 64 | +
|
| 65 | + return 0; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Requirements |
| 70 | + |
| 71 | +|Macro|Required header| |
| 72 | +|-------------|---------------------| |
| 73 | +|**`static_assert`**|\<assert.h>| |
| 74 | + |
| 75 | +## See also |
| 76 | + |
| 77 | +[_STATIC_ASSERT Macro](../c-runtime-library/reference/static-assert-macro.md)\ |
| 78 | +[assert macro and _assert and _wassert functions](../c-runtime-library/reference/assert-macro-assert-wassert.md) |
| 79 | +[/std (Specify language standard version)](../build/reference/std-specify-language-standard-version.md) |
0 commit comments