Machine-translated by Google. Questions are authored in English.
Top

Discussion

#include <stdio.h>
#if X == 3
    #define Y 3
#else
    #define Y 5
#endif
 
int main()
{
    printf("%d", Y);
    return 0;
}

What is the output of the above program?

  • A.3
  • B.5
  • C.3 or 5 depending on value of X
  • D.Compile time error

Answer: B

Correct answer: 5

X is never defined, and in a #if any identifier that is not a defined macro is replaced by 0. So the test reads 0 == 3, which is false, the #else branch is taken, Y becomes 5, and the program prints 5. Note this is decided at preprocessing time, so it cannot depend on any run-time value.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.