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); }- A. 1, because #if defined always evaluates true
- B. Compilation error: DEBUG undeclared
- C. 1, since const defaults to debug mode
- 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
- What is printed? c #define ISODD(x) (x % 2 == 1) int main(void) { printf("%d", ISODD(4 - 1)); }
- What is printed? c #define STR(x) #x #define XSTR(x) STR(x) #define VERSION 7 int main(void) { printf("%s %s", STR(VERSION)…
- What is printed? c #define SWAP(a, b) do { int t = a; a = b; b = t; } while (0) int main(void) { int x = 1, y = 2; if (x < y) SWAP(x, y)…
- What is printed? c #define A 1 #define B (A + 1) #undef A #define A 10 int main(void) { printf("%d", B); }
- What is printed? c #define FOO 10 #define BAR FOO + 5 int main(void) { int x = BAR 2; printf("%d", x); }
- What is printed? c #define LOG(msg) printf("[LOG] %s line %dn", msg, LINE) int main(void) { LOG("start"); LOG("end"); }