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);
  1. A. 3 20 2 30
  2. B. 2 20 2 30
  3. C. 2 20 3 30
  4. 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

Exams that ask this topic