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) ?

+7
source share
2 answers

From here :

Pragma directives define machine- or operating system-specific compiler functions. The __pragma keyword, specific to the Microsoft compiler , allows you to encode pragma directives in macro definitions.

Also (same link):

Keyword __pragma ()

Microsoft specific

The compiler also supports the __pragma keyword, which has the same functionality as the #pragma , but can be used inline in the macro definition. The #pragma cannot be used in macro definitions because the compiler interprets the number sign character ('#') in the directive as a line operator (#).

In __pragma() you can always use #pragma instead of __pragma() . There is no need to use __pragma() , but sometimes it can be used.

+8
source

The _Pragma operator _Pragma introduced in C99 . _Pragma(arg) is an operator very similar to sizeof or defined , and can be embedded in macros.

At cpp.gnu.org :

Its syntax is _Pragma ( string-literal ) , where a literal string can be a regular or wide character string. It is destroyed, replacing everything \\ one \ and everything \" by " . Then the result is processed as if it were displayed on the right side of the #pragma . For instance,

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

has the same effect as #pragma GCC dependency "parse.y" . The same effect can be achieved with 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 IBM 2010 string is inserted into the C ++ object file when compiling the following code:

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

For more information about _pragma with an example.

+12
source

Source: https://habr.com/ru/post/1270519/


All Articles