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);- A. Compilation error
- B. high 2
- C. high 0
- 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
- What is printed? c struct E { char name[8]; int age; }; struct E t[] = {{"Asha", 31}, {"Bilal", 27}, {"Chen", 45}, {"Dev", 22}}; struct E…
- What is printed? c int f(int a, int n) { if (n == 1) return a[0]; int m = f(a + 1, n - 1); return a[0] > m ? a[0] - m : m - a[0]; } int…
- What is printed? c printf("%.1f", 1 ? 3 : 2.5);
- What is printed? c int a = 0, b = 10; while (a < b) { a += 3; b -= 2; } printf("%d %d", a, b);
- After int q = realloc(p, 2 n sizeof q); succeeds (q is not NULL), where p pointed to n ints, which statement is true?
- With int i = 1, x; int a[5] = {0}; which of these statements have undefined behaviour? (i) i = i++ + 1; (ii) a[i] = i++; (iii) i = (i, i +…