|
| 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) |
0 commit comments