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
* start draft
* links
* link
* acrolinx and link
* tech review
* comment
* link broken by heading change
* tech review
* shortened up the trailing slashes
Co-authored-by: TylerMSFT <[email protected]>
@@ -64,7 +64,7 @@ Starting in Visual Studio 2019 version 16.8, you may specify **`/std:c11`** or *
64
64
65
65
When you specify **`/std:c11`** or **`/std:c17`**, MSVC supports all the required features of C11 and C17. The compiler options enable support for these functionalities:
> **`__pragma(`***token-string***`)`** // two leading underscores - Microsoft specific extension
17
+
> **`_Pragma(`***string-literal***`)`** // C99
16
18
17
19
## Remarks
18
20
19
21
Each implementation of C and C++ supports some features unique to its host machine or operating system. Some programs, for example, must exercise precise control over the location of data in memory, or control the way certain functions receive parameters. The **#pragma** directives offer a way for each compiler to offer machine- and operating system-specific features, while maintaining overall compatibility with the C and C++ languages.
20
22
21
23
Pragmas are machine- or operating system-specific by definition, and are typically different for every compiler. Pragmas can be used in conditional directives, to provide new preprocessor functionality, or to provide implementation-defined information to the compiler.
22
24
23
-
The *token-string* is a series of characters that gives a specific compiler instruction and arguments, if any. The number sign (**#**) must be the first non-white-space character on the line that contains the pragma. White-space characters can separate the number sign and the word "pragma". Following **#pragma**, write any text that the translator can parse as preprocessing tokens. The argument to **#pragma** is subject to macro expansion.
25
+
The *token-string* is a series of characters representing a specific compiler instruction and arguments, if any. The number sign (**#**) must be the first non-white-space character on the line that contains the pragma. White-space characters can separate the number sign and the word "pragma". Following **#pragma**, write any text that the translator can parse as preprocessing tokens. The argument to **#pragma** is subject to macro expansion.
26
+
27
+
The *string-literal* is the input to `_Pragma`. Outer quotes and leading/trailing whitespace are removed. `\"` is replaced with `"` and `\\` is replaced with `\`.
24
28
25
29
The compiler issues a warning when it finds a pragma that it doesn't recognize, and continues compilation.
26
30
@@ -99,9 +103,9 @@ cl /Zp8 some_file.cpp
99
103
100
104
## The __pragma() keyword
101
105
102
-
The compiler also supports the Microsoft-specific **__pragma** keyword, which has the same functionality as the **#pragma** directive. The difference is, the **__pragma** keyword is usable inline in a macro definition. The **#pragma** directive isn't usable in a macro definition, because the compiler interprets the number sign character ('#') in the directive as the [stringizing operator (#)](../preprocessor/stringizing-operator-hash.md).
106
+
The compiler also supports the Microsoft-specific **`__pragma`** keyword, which has the same functionality as the **`#pragma`** directive. The difference is, the **`__pragma`** keyword is usable inline in a macro definition. The **`#pragma`** directive isn't usable in a macro definition, because the compiler interprets the number sign character ('#') in the directive as the [stringizing operator (#)](../preprocessor/stringizing-operator-hash.md).
103
107
104
-
The following code example demonstrates how the **__pragma** keyword can be used in a macro. This code is excerpted from the mfcdual.h header in the ACDUAL sample in "Compiler COM Support Samples":
108
+
The following code example demonstrates how the **`__pragma`** keyword can be used in a macro. This code is excerpted from the *mfcdual.h* header in the ACDUAL sample in "Compiler COM Support Samples":
105
109
106
110
```cpp
107
111
#defineCATCH_ALL_DUAL\
@@ -121,6 +125,48 @@ END_CATCH_ALL \
121
125
return _hr; \
122
126
```
123
127
128
+
## The `_Pragma` preprocessing operator (C99, C++11)
129
+
130
+
`_Pragma` is similar to the Microsoft-specific [`__pragma`](#the-__pragma-keyword) keyword, except it's part of the standard. It was introduced for C in C99. For C++, it was introduced in C++11.
131
+
132
+
It allows you to put pragmas into a macro definition. It has one leading underscore `_` instead of two leading underscores `__` that the Microsoft-specific keyword has, and the first letter is capitalized.
133
+
134
+
The string literal should be what you would otherwise put following a *`#pragma`* statement. For example:
135
+
136
+
```c
137
+
#pragma message("--the #pragma way")
138
+
_Pragma ("message( \"the _Pragma way\")")
139
+
```
140
+
141
+
Quotation marks and back-slashes should be escaped, as shown above. A pragma string that isn't recognized is ignored.
142
+
143
+
The following code example demonstrates how the **`_Pragma`** keyword could be used in an assert-like macro when you don't want to get a warning when the condition expression happens to be constant.
144
+
145
+
The macro definition uses the do/while(0) idiom for multi-statement macros so that it can be used as though it were one statement. See [C multi-line macro](https://stackoverflow.com/questions/1067226/c-multi-line-macro-do-while0-vs-scope-block) on Stack Overflow for more info. The _Pragma statement only applies to the line of code that follows it.
146
+
147
+
```C
148
+
// Compile with /W4
149
+
150
+
#include<stdio.h>
151
+
#include<stdlib.h>
152
+
153
+
#defineMY_ASSERT(BOOL_EXPRESSION) \
154
+
do { \
155
+
_Pragma("warning(suppress: 4127)") /* C4127 conditional expression is constant */ \
156
+
if (!(BOOL_EXPRESSION)) { \
157
+
printf("MY_ASSERT FAILED: \"" #BOOL_EXPRESSION "\" on %s(%d)", __FILE__, __LINE__); \
158
+
exit(-1); \
159
+
} \
160
+
} while (0)
161
+
162
+
intmain()
163
+
{
164
+
MY_ASSERT(0 && "Note that there is no warning: C4127 conditional expression is constant");
0 commit comments