ProgrammingVery hardProduct company OA
What is printed?
What is printed?
#define A 1
#define B
#if defined(B) && A + C
#define X 10
#else
#define X 20
#endif
#undef A
#ifdef A
#define Y 1
#elif !defined(C) && X > 15
#define Y 2
#else
#define Y 3
#endif
int main(void) { printf("%d %d", X, Y); }- A. 20 2
- B. 10 1
- C. 10 2
- D. 10 3
Correct answer: D. 10 3
B is defined (as nothing), so defined(B) is 1. In #if arithmetic any identifier that is not a macro is replaced by 0, so A + C = 1 + 0 is true and X = 10; an undefined C is not an error. After #undef A the #ifdef fails; the #elif needs X > 15, which is false for 10, so the #else gives Y = 3. '20 2' assumes an empty macro counts as undefined or that C makes the #if fail.
More C Programming questions
- An array of int that may contain any values from INTMIN to INTMAX is sorted with qsort. With int x = (const int )a, y = (const int )b…
- What is printed? c int a[] = {4, 8, 15, 16, 23}; printf("%zu %d", sizeof(a) / sizeof(a[0]), a[sizeof(a) / sizeof(a[0]) - 1]);
- What is printed? c printf("%d %d %d", 5 > 3 && 2 > 4, !0, !!7);
- What is printed? c int a = 5, b = 3; a = a ^ b; b = a ^ b; a = a ^ b; printf("%d %d", a, b);
- What is printed? c printf("%d %d %d", strcmp("abc", "abd") < 0, strcmp("ab", "abc") < 0, strncmp("abcx", "abcy", 3) == 0);
- What is printed? c int i = 3.99; int j = -3.99; int k = (int)(0.29 100); printf("%d %d %d", i, j, k);