1

I just know that #pragma once is not part of the C standard but rather part of the preprocessor.
In that case, does that make the preprocessor not part of the C standard?
So, are the C programming language and the C preprocessor different entities?

1
  • 2
    The C preprocessor is a part of the C standard. But #pragma once is not (maybe it will be in the future). Other preprocessor directives like #include ..., #ifdef ... etc are definitely a part of the standard. Commented Nov 24, 2024 at 7:25

2 Answers 2

2

The preprocessor is very much a part of the C standard — see C11 §6.10 Preprocessing directives for the details in C11. The C standard and the preprocessor are not different entities. C23 has many new features in the preprocessor — perhaps the most significant is __VA_OPT__ (though the #embed directive takes up the most space in the standard).

You are correct that #pragma once is not part of Standard C. However, a number of compilers provide support for it. That means those compilers provide an extension to the C standard — and any compiler usually provides a number of extensions (often a large number of extensions) to the C standard.

Sign up to request clarification or add additional context in comments.

2 Comments

Re "And I don't think it is what the OP is asking about.", Yet your answer is answered by a simple check of the spec. So yeah, the OP was asking us to check the spec for them.
@ikegami: That's one part of the question, but the standard doesn't discuss much about how implementations might extend the standard, and that misunderstanding needed to be addressed separately.
0

There's not really a formal term called "preprocessor". The C language uses the term preprocessing for things that are done in the early stages of compilation - the C standard speaks of translation phases, where the source code for now consists of "preprocessing tokens" to be handled before later on translating everything to "tokens" that are analyzed for C syntax and meaning.

Most compilers offer an option to get "preprocessor output", which might lead to some people getting the wrong idea that the preprocessor is some separate executable like for example a linker or debugger. But the preprocessor is in fact a mandatory part integrated with everything else in the compiler and everything done at the preprocessing stages is strictly specified by the C standard.

As for #pragma, it is a standard preprocessing directive used for situations like: "if we don't recognize the things written in a pragma, simply ignore it", allowing compiler-specific commands to get entered and still pass compilation while using other compilers not recognizing the command. So if I write something like #pragma DONALD DUCK then all compilers must still accept the code, even though the text holds no meaning to them. Some compilers may decide to give a warning for unknown pragmas.

Therefore everything following #pragma is, by the very nature of that command, implementation-defined, meaning compiler-specific. once is one such command implemented by a couple of compilers as an extension. When people say it is "not standard" they actually mean that it's a non-portable feature, since the very same can be achieved with 100% portable code: #ifndef HEADER_H #define HEADER_H ... #endif. All compilers must implement that in the same way. But they are by no means obliged to implement #pragma once, why it is sometimes considered bad practice to use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.