ProgrammingHardAccenture · Cognizant pattern
What is printed?
What is printed?
#define INCR(x) x++
int main() {
int i = 5;
int r = 2 * INCR(i);
printf("%d %d", r, i);
}- A. 12 6
- B. 10 6
- C. Compilation error: cannot use postfix ++ inside a macro argument this way
- D. 11 6
Correct answer: D. 11 6
INCR(i) expands textually to i++, so `2 * INCR(i)` becomes `2 * i++`. Postfix ++ yields i's ORIGINAL value (5) for the multiplication (2*5=11) and THEN increments i to 6 as a side effect.
More C Programming questions
- What is printed? c #define PR(x) printf(#x " = %dn", x) int main() { int value = 42; PR(value); }
- What is printed? c #define CONCAT(a, b) a##b int main() { int xy = 100; printf("%d", CONCAT(x, y)); }
- What is printed? c #define ISEVEN(n) ((n) % 2 == 0) int main() { int x = 7; if (!ISEVEN(x)) printf("odd"); else printf("even"); }
- Assume 32-bit int. What is printed? c int flags = 0; flags |= (1 << 2); flags |= (1 << 4); flags &= ~(1 << 2); printf("%d", flags);
- What is printed? c #if defined(DEBUG) && !defined(RELEASE) #define LOG(x) printf("[debug] %sn", x) #else #define LOG(x) #endif int main()…
- What is printed? c int a = 10; int b = (a > 5) ? (a < 20 ? 1 : 2) : 3; printf("%d", b);