Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is #pragma considered a preprocessor directive?

I know it starts with #, and it follows the preprocessing directives format. But does the preprocessor really care about it? The #pragma pack, #pragma once, and all other directives I know, are all proceeded by compilers. A comment in this question even states that #pragma directive survives the pre-processing stage. So my questions are:

  1. Since #pragma is for compiler, why is it considered a preprocessing directive? Is it just because it starts with #?
  2. Does preprocessor really do something with #pragma?
like image 654
user239216 Avatar asked Sep 07 '20 08:09

user239216


Video Answer


1 Answers

Why is #pragma considered a preprocessor directive?

Because the C standard says so. It is specified in the chapter preprocessing directives, C17 6.10.6. Other than that, the standard is intentionally very vague with what #pragma should do, since the whole purpose is to do something compiler-specific. Or in case the pragma isn't recognized - ignore it.

How a certain compiler handles the contents of a pragma internally isn't specified.

Some pragmas obviously need to be pre-processed, notably the kind that enables/disables certain compiler behavior like #pragma warning ... etc. Lots of them must be evaluated during pre-processing or the compiler won't know how to compile the code.

Does preprocessor really do something with #pragma?

Yes, it evaluates it in translation phase 4: "Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed."

Please note that having a pre-processor separated from the compiler is mostly a theoretical model. In reality the pre-processor and compiler are often rather tightly integrated with each other.

like image 122
Lundin Avatar answered Sep 18 '22 02:09

Lundin