ProgrammingHardProduct company OA

What is printed?

What is printed?
#define LEVEL 2
#if LEVEL > 1
#define MSG "high"
#elif LEVEL > 0
#define MSG "low"
#endif
#undef LEVEL
#ifndef LEVEL
#define LEVEL 0
#endif
printf("%s %d", MSG, LEVEL);
  1. A. Compilation error
  2. B. high 2
  3. C. high 0
  4. D. low 0

Correct answer: C. high 0

The #if is decided once, when the preprocessor reaches it, with LEVEL = 2, so MSG is "high". Later #undef removes LEVEL, #ifndef is then true and LEVEL is redefined as 0; MSG is unaffected because it was already defined as a literal.

More C Programming questions

Exams that ask this topic