ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
#if defined(DEBUG)
const int mode = 1;
#else
const int mode = 0;
#endif
int main(void) { printf("%d", mode); }
  1. A. 1, because #if defined always evaluates true
  2. B. Compilation error: DEBUG undeclared
  3. C. 1, since const defaults to debug mode
  4. D. 0, since DEBUG is never defined in this translation unit

Correct answer: D. 0, since DEBUG is never defined in this translation unit

defined(DEBUG) checks whether the macro DEBUG was defined (via #define or a -D compiler flag) before this point; since no such definition appears, the #else branch is compiled, setting mode to 0.

More C Programming questions

Exams that ask this topic