ProgrammingModerateTCS NQT · Infosys pattern
What is printed?
What is printed?
#define DOUBLE(x) x + x
printf("%d", 10 / DOUBLE(5) * 2);- A. 12
- B. 7
- C. 2
- D. 1
Correct answer: A. 12
Text substitution gives 10 / 5 + 5 * 2. With precedence that is (10 / 5) + (5 * 2) = 2 + 10 = 12, not 10 / 10 × 2 = 2. Always parenthesise the whole macro body and each parameter.
More C Programming questions
- Which of a, b, c, d are pointers? c #define IPTR int typedef int IPT; IPTR a, b; IPT c, d;
- What is printed? c #define DEBUG 0 ... #ifdef DEBUG printf("on "); #endif #if DEBUG printf("trace"); #endif
- In C, with int i = 1, a[5];, which statement has undefined behaviour?
- Compiled as C with 4-byte int, what is printed? c printf("%zu %zu", sizeof('A'), sizeof(char));
- What is printed? c int x = 1; { int x = 2; { x = 3; } printf("%d ", x); } printf("%d", x);
- With 4-byte int, what is printed? c int a[5]; printf("%td %td", (char )(a + 1) - (char )a, (char )(&a + 1) - (char )a);