Reputation: 728
Do c/c++ preprocessors process all lines that begin with #? Does is errors out when encountering unknown macros or will it just ignore them?
for an example,
#include <stdio.h>
#hello
int main(){
printf("Hello World!");
return 0;
}
what happens in this situation?will it produce an error or will it work (ignoring #hello line)?
Upvotes: 1
Views: 2030
Reputation: 238341
The language grammar specifies all pre-processor directives that exist in the language. If you use any other name for the directive, then that is a "conditionally-supported-directive". If the conditionally supported directive isn't supported, then the the language implementation is required to issue a diagnostic message and is free to refuse to proceed.
Upvotes: 6
Reputation: 17403
Syntactically, #hello
is a "non-directive" preprocessing directive.
C17/C18 section 6.10 paragraph 9 (newly added in C17/C18) says:
The execution of a non-directive preprocessing directive results in undefined behavior.
"Undefined behavior" does not necessarily mean that the compiler will fail to translate the code or issue a diagnostic. (EDIT: As pointed out by Eric Postpischil in the comments, execution of a non-directive preprocessing directive does not violate a constraint so a diagnostic is not required.) It could behave in a documented manner, for example if the directive is part of an extension to the C language.
Syntactically, #hello
is a "conditionally-supported-directive" preprocessing directive.
C++20 section 15.1 paragraph 2 says:
A conditionally-supported-directive is conditionally-supported with implementation-defined semantics.
"Conditionally-supported" means that an implementation is not required to support it. Implementations need to document all conditionally-supported constructs that they do not support. (In the case of conditionally-supported-directives, I guess that would amount to documenting that none of them are supported, or documenting the semantics of those that are supported.)
Upvotes: 5
Reputation: 49
An unrecognized preprocessing directive error will rise, and your code won't compile
Upvotes: 0