ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
#define VERSION 2
#if VERSION >= 2
const char *msg = "new";
#else
const char *msg = "old";
#endif
int main(void) { printf("%s", msg); }- A. new
- B. old
- C. Compilation error: #if cannot compare macros
- D. Undefined behaviour
Correct answer: A. new
#if evaluates constant integer expressions at preprocessing time; VERSION expands to 2, and 2 >= 2 is true, so the first branch (msg = "new") is compiled in and printed.
More C Programming questions
- What is printed? c #define TWICE(x) (2 (x)) #define FOURTIMES(x) TWICE(TWICE(x)) printf("%d", FOURTIMES(3));
- What is printed? c #define ISEVEN(n) ((n) % 2 == 0) int a = 7; printf("%d", ISEVEN(a + 1));
- What is printed? c #define EMPTY #define SEMI ; int x = 5 EMPTY SEMI printf("%d", x);
- What is printed? c #define PRINT(x) printf(#x " = %dn", x) int main(void) { int value = 42; PRINT(value); }
- What is printed? c #define MIN(a, b) ((a) < (b) ? (a) : (b)) int calls = 0; int next(void) { calls++; return calls; } int main(void) { int…
- What is printed? c #if defined(DEBUG) const int mode = 1; #else const int mode = 0; #endif int main(void) { printf("%d", mode); }