What is the value of an undefined constant used in #if?

C++C Preprocessor

C++ Problem Overview


My preprocessor appears to assume that undefined constants are 0 for the purpose of evaluating #if conditions.

Can this be relied upon, or do undefined constants give undefined behaviour?

C++ Solutions


Solution 1 - C++

Yes, it can be relied upon. The C99 standard specifies at §6.10.1 ¶3: > After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers are replaced with the pp-number 0

Edit

Sorry, I thought it was a C question; still, no big deal, the equivalent section in the C++ standard (§16.1 ¶4) states:

>After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers and keywords, except for true and false, are replaced with the pp-number 0

The only difference is the different handling of true and false, which in C do not need special handling, while in C++ they have a special meaning even in the preprocessing phase.

Solution 2 - C++

An identifier that is not defined as a macro is converted to 0 before the expression is evaluated.

The exception is the identifier true, which is converted to 1. This is specific to the C++ preprocessor; in C, this doesn't happen and you would need to include <stdbool.h> to use true this way, in which case it will be defined as a macro and no special handling is required.

Solution 3 - C++

The OP was asking specifically about the C preprocessor and the first answer was correctly referring to the C preprocessor specification. But some of the other comments seem to blur the distinction between the C preprocessor and the C compiler. Just to be clear, those are two different things with separate rules and they are applied in two separate passes.

#if 0 == NAME_UNDEFINED
int foo = NAME_UNDEFINED;
#endif

This example will successfully output the foo definition because the C preprocessor evaluates NAME_UNDEFINED to 0 as part of a conditional expression, but a compiler error is generated because the initializer is not evaluated as a conditional expression and then the C compiler evaluates it as an undefined symbol.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTony ParkView Question on Stackoverflow
Solution 1 - C++Matteo ItaliaView Answer on Stackoverflow
Solution 2 - C++James McNellisView Answer on Stackoverflow
Solution 3 - C++jsheaneyView Answer on Stackoverflow