-1
    #include<stdio.h>
    #define Y 10
    int main()
    {
    #if X && Y || Z
    printf ("A\n");
    #else
    printf("B\n");
    #endif
    }

Here else is satisfied B is printed but if I use

#if !X && Y || Z

If is satisfied and A is printed

My question is, If X and Z isn't defined, how's pre-processor solving the statement?

1 Answers1

3

Unresolved symbols in a preprocessor conditional are treated as zeros. Hence when neither X nor Z is defined, both are treated as zero.

See C11 §6.10.1 Conditional inclusion ¶4:

After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers (including those lexically identical to keywords) are replaced with the pp-number 0, and then each preprocessing token is converted into a token.

A preprocessing number (or pp-number) is more general than a number in the main language.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278