A common assumption is that preprocessors interpret undefined constants as 0 when used in #if conditions. But can this be consistently relied upon, or does it yield unpredictable results?
The answer lies in the C99 and C 17 standards.
C99 Standard
The C99 standard specifies in §6.10.1 ¶3 that:
"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."
C 17 Standard
Similarly, the C 17 standard states in §16.1 ¶4:
"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."
Conclusion
Therefore, it is indeed reliable in both C and C to assume that undefined constants are evaluated as 0 in #if conditions. This behavior is explicitly defined in the language standards, ensuring consistent execution across different compilers and platforms.
The above is the detailed content of Are Undefined Constants Evaluated as 0 in C and C `#if` Macros?. For more information, please follow other related articles on the PHP Chinese website!