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

Tony Park picture Tony Park · Feb 23, 2011 · Viewed 8.7k times · Source

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?

Answer

Matteo Italia picture Matteo Italia · Feb 23, 2011

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.