Skip to content

Commit 651348f

Browse files
authored
Merge pull request #3208 from MicrosoftDocs/master
10/14/2020 AM Publish
2 parents 43cee7a + a89998f commit 651348f

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

docs/c-language/c-keywords.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "C Keywords"
33
description: "Keywords in Standard C and Microsoft C compiler extensions."
4-
ms.date: 09/12/2020
4+
ms.date: 10/12/2020
55
helpviewer_keywords: ["keywords [C]", "redefining keywords", "Microsoft-specific keywords"]
66
ms.assetid: 2d932335-97bf-45cd-b367-4ae00db0ff42
77
---
@@ -110,7 +110,8 @@ The following keywords and special identifiers are recognized by the Microsoft C
110110
**`__try`**<sup>5</sup>\
111111
**`dllexport`**<sup>4</sup>\
112112
**`__inline`**<sup>5</sup>\
113-
**`__leave`**<sup>5</sup>
113+
**`__leave`**<sup>5</sup>\
114+
**`static_assert`**<sup>6</sup>
114115
:::column-end:::
115116
:::row-end:::
116117

@@ -120,6 +121,8 @@ The following keywords and special identifiers are recognized by the Microsoft C
120121

121122
<sup>5</sup> For compatibility with previous versions, these keywords are available both with two leading underscores and a single leading underscore when Microsoft extensions are enabled.
122123

124+
<sup>6</sup> When <assert.h> is not included, the Microsoft Visual C compiler maps **`static_assert`** to the C11 **`_Static_assert`** keyword.
125+
123126
Microsoft extensions are enabled by default. To assist in creating portable code, you can disable Microsoft extensions by specifying the [/Za \(Disable language extensions)](../build/reference/za-ze-disable-language-extensions.md) option during compilation. When you use this option, some Microsoft-specific keywords are disabled.
124127

125128
When Microsoft extensions are enabled, you can use the keywords listed above in your programs. For standards compliance, most of these keywords are prefaced by a double underscore. The four exceptions, **`dllexport`**, **`dllimport`**, **`naked`**, and **`thread`**, are used only with **`__declspec`** and don't require a leading double underscore. For backward compatibility, single-underscore versions of the rest of the keywords are supported.

docs/c-language/static-assert-c.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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)

docs/c-language/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@
400400
href: ../c-language/null-statement-c.md
401401
- name: return statement (C)
402402
href: ../c-language/return-statement-c.md
403+
- name: static_assert statement (C11)
404+
href: ../c-language/static-assert-c.md
403405
- name: switch statement (C)
404406
href: ../c-language/switch-statement-c.md
405407
- name: try-except statement (C)

0 commit comments

Comments
 (0)