ProgrammingModerateAccenture · Capgemini pattern

What is printed?

What is printed?
struct P { int x, y; } p = {3, 4}, *q = &p;
q->x += q->y;
(*q).y *= 2;
printf("%d %d", p.x, p.y);
  1. A. 3 4
  2. B. 7 8
  3. C. 7 4
  4. D. 3 8

Correct answer: B. 7 8

q points at p itself (not a copy), and q->x is shorthand for (*q).x. So p.x becomes 3 + 4 = 7 and then p.y becomes 4 × 2 = 8. The brackets in (*q).y are needed because . binds tighter than unary *.

More C Programming questions

Exams that ask this topic