You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 6fec72c
Browse filesBrowse the repository at this point in the historyBrowse files
# _Static_assert keyword and static_assert macro (C11)
10
10
11
-
New in C11. 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.
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
12
13
13
**`_Static_assert`** is a keyword introduced in C11.
14
14
**`static_assert`** is a macro, introduced in C11, that maps to the **`_Static_assert`** keyword.
An integral constant expression that can be evaluated at compile time. If the evaluated expression is zero (false), the *string-literal* parameter is displayed and the compilation fails with an error. If the expression is nonzero (true), then there's no effect.
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
27
28
28
*string-literal*\
29
-
The message to display if the *constant-expression* evaluates to zero (false). The message is a string of characters using the [base character set](../c-language/ascii-character-set.md) of the compiler; that is, not[multibyte or wide characters](../c-language/multibyte-and-wide-characters.md).
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
30
31
31
## Remarks
32
32
33
-
The **`_Static_assert`** keyword, and the **`static_assert`** macro, test a software assertion at compile time. They can be used at global or function scope.
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
34
35
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
36
37
37
**Microsoft-specific behavior**\
38
38
39
-
In C, if you don't include <assert.h>, the Microsoft Visual C/C++ compiler treats **`static_assert`** as a keyword that maps to **`_Static_assert`**. The reason for this mapping is that C++ has a **`static_assert`** keyword.
40
-
41
-
Using the **`static_assert`** macro is preferred because the same code will work in both C and C++.
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 the **`static_assert`** macro is preferred because the same code will work in both C and C++.
42
40
43
41
## Example of a compile-time assert
44
42
45
-
In the following example, **`static_assert`** and **`_Static_assert`** are used to verify that the `Items` enum only contains three elements, and that integers are 32 bits wide.
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.
0 commit comments