Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
776 views
in Technique[技术] by (71.8m points)

macros - Difference between #pragma and _Pragma() in C

What is the difference between #pragma and _Pragma() in C?

syntax:

#pragma arg

and

_Pragma(arg)

When should I use _Pragma(arg)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

_Pragma operator introduced in C99. _Pragma(arg) is an operator, much like sizeof or defined, and can be embedded in a macro.

According to cpp.gnu.org reference:

Its syntax is _Pragma (string-literal), where string-literal can be either a normal or wide-character string literal. It is destringized, by replacing all \ with a single and all " with a ". The result is then processed as if it had appeared as the right hand side of a #pragma directive. For example,

 _Pragma ("GCC dependency "parse.y"")

has the same effect as #pragma GCC dependency "parse.y". The same effect could be achieved using macros, for example

 #define DO_PRAGMA(x) _Pragma (#x)
 DO_PRAGMA (GCC dependency "parse.y")

According to IBM tutorial:

The _Pragma operator is an alternative method of specifying #pragma directives. For example, the following two statements are equivalent:

#pragma comment(copyright, "IBM 2010")
_Pragma("comment(copyright, "IBM 2010")")

The string IBM 2010 is inserted into the C++ object file when the following code is compiled:

_Pragma("comment(copyright, "IBM 2010")")
int main() 
{
   return 0;
}

For more information about _pragma with example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...