ProgrammingVery hardProduct company OA · GATE CS
What is printed?
What is printed?
struct S { int a; int b; } s[] = {{1, 10}, {2, 20}, {3, 30}};
struct S *p = s;
int x = ++p->a;
int y = (++p)->b;
int z = p++->a;
printf("%d %d %d %d", x, y, z, p->b);- A. 3 20 2 30
- B. 2 20 2 30
- C. 2 20 3 30
- D. 2 10 2 30
Correct answer: B. 2 20 2 30
-> binds tighter than prefix ++, so ++p->a increments s[0].a to 2 and leaves p alone. (++p)->b moves p to s[1] and reads 20. p++->a reads s[1].a = 2 and then moves p to s[2], whose b is 30. '3 20 …' would need p to have moved in the first line.
More C Programming questions
- Assume 32-bit unsigned int. What is printed? c unsigned x = 0x12345678; printf("%X", (x >> 16) | (x << 16));
- What is printed? c int ok(const char s, int d) { if (d < 0) return 0; if (!s) return d == 0; return ok(s + 1, d + (s == '(') - (s ==…
- What is printed? c int n = 1200, r = 0; while (n) { r = r 10 + n % 10; n /= 10; } printf("%d", r);
- What is printed? c enum { R = 4, W = 2, X = 1 }; int perm = R | X; perm |= W; perm &= ~R; perm ^= X; printf("%d %d", perm, (perm & W) != 0);
- What is printed? c printf("%d", 5["abcdef"] - 'a');
- What is printed? c const char s = ""; int n = 0; for (int i = 0; i < strlen(s) - 1; i++) { n++; if (n > 5) break; } printf("%d", n);