ProgrammingVery hardProduct company OA

What is printed?

What is printed?
#define A 1
#define B
#if defined(B) && A + C
#define X 10
#else
#define X 20
#endif
#undef A
#ifdef A
#define Y 1
#elif !defined(C) && X > 15
#define Y 2
#else
#define Y 3
#endif
int main(void) { printf("%d %d", X, Y); }
  1. A. 20 2
  2. B. 10 1
  3. C. 10 2
  4. D. 10 3

Correct answer: D. 10 3

B is defined (as nothing), so defined(B) is 1. In #if arithmetic any identifier that is not a macro is replaced by 0, so A + C = 1 + 0 is true and X = 10; an undefined C is not an error. After #undef A the #ifdef fails; the #elif needs X > 15, which is false for 10, so the #else gives Y = 3. '20 2' assumes an empty macro counts as undefined or that C makes the #if fail.

More C Programming questions

Exams that ask this topic