Home  >  Article  >  Backend Development  >  Are Undefined Constants Evaluated as 0 in C and C `#if` Macros?

Are Undefined Constants Evaluated as 0 in C and C `#if` Macros?

Susan Sarandon
Susan SarandonOriginal
2024-11-10 16:29:02668browse

Are Undefined Constants Evaluated as 0 in C and C   `#if` Macros?

Is an Undefined Constant Evaluated as 0 in C and C #if Macros?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn