Skip to content

Commit 40846a7

Browse files
authored
Merge pull request #3216 from TylerMSFT/twhitney-noreturn
draft for noreturn
2 parents 4edd986 + eeb17fd commit 40846a7

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

docs/c-language/c-keywords.md

Lines changed: 2 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: 10/12/2020
4+
ms.date: 10/15/2020
55
helpviewer_keywords: ["keywords [C]", "redefining keywords", "Microsoft-specific keywords"]
66
ms.assetid: 2d932335-97bf-45cd-b367-4ae00db0ff42
77
---
@@ -121,7 +121,7 @@ The following keywords and special identifiers are recognized by the Microsoft C
121121

122122
<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.
123123

124-
<sup>6</sup> When <assert.h> is not included, the Microsoft Visual C compiler maps **`static_assert`** to the C11 **`_Static_assert`** keyword.
124+
<sup>6</sup> If you don't include <assert.h>, the Microsoft Visual C compiler maps **`static_assert`** to the C11 **`_Static_assert`** keyword.
125125

126126
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.
127127

docs/c-language/noreturn.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: "_Noreturn keyword and noreturn macro (C11)"
3+
description: "Describes the `_Noreturn` keyword and `noreturn` macro."
4+
ms.date: 10/16/2020
5+
f1_keywords: ["_Noreturn_c", "noreturn"]
6+
helpviewer_keywords: ["keywords [C]"]
7+
---
8+
9+
# `_Noreturn` keyword and `noreturn` macro (C11)
10+
11+
The `_Noreturn` keyword was introduced in C11. It tells the compiler that the function it's applied to doesn't return. The compiler knows that the code following a call to a `_Noreturn` function is unreachable.
12+
13+
A convenience macro, `noreturn`, provided in <stdnoreturn.h>, maps to the `_Noreturn` keyword.
14+
15+
The primary benefits for using `_Noreturn` (or the equivalent `noreturn`) are making the intention of the function clear in the code for future readers, and detecting unintentionally unreachable code.
16+
17+
## Example using `noreturn` macro and `_Noreturn `keyword
18+
19+
The following example demonstrates the `_Noreturn` keyword and the equivalent `noreturn` macro.
20+
21+
IntelliSense may generate a spurious error, `E0065`, if you use the macro `noreturn` that you can ignore. It doesn't prevent you from running the sample.
22+
23+
```C
24+
// Compile with Warning Level4 (/W4) and /std:c11
25+
#include <stdio.h>
26+
#include <stdlib.h>
27+
#include <stdnoreturn.h>
28+
29+
noreturn void fatal_error(void)
30+
{
31+
exit(3);
32+
}
33+
34+
_Noreturn void not_coming_back(void)
35+
{
36+
puts("There's no coming back");
37+
fatal_error();
38+
return; // warning C4645 - function declared with noreturn has a return statement
39+
}
40+
41+
void done(void)
42+
{
43+
puts("We'll never get here");
44+
}
45+
46+
int main(void)
47+
{
48+
not_coming_back();
49+
done(); // warning c4702 - unreachable code
50+
51+
return 0;
52+
}
53+
```
54+
55+
## Requirements
56+
57+
|Macro|Required header|
58+
|-------------|---------------------|
59+
|**`noreturn`**|\<stdnoreturn.h>|
60+
61+
## See also
62+
63+
[/std (Specify language standard version)](../build/reference/std-specify-language-standard-version.md)\
64+
[/W4 (Specify warning level)](../build/reference/compiler-option-warning-level.md)
65+
[C4702 warning](../error-messages\compiler-warnings\compiler-warning-level-4-c4702.md)

docs/c-language/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@
434434
href: ../c-language/inline-functions.md
435435
- name: Inline assembler (C)
436436
href: ../c-language/inline-assembler-c.md
437+
- name: _Noreturn (C)
438+
href: ../c-language/noreturn.md
437439
- name: DLL import and export functions
438440
items:
439441
- name: DLL import and export functions

0 commit comments

Comments
 (0)