ProgrammingHardProduct company OA · GATE CS

What is printed?

What is printed?
struct P { int x; int y; } p = {7, 9};
int *q = (int *)&p;
printf("%d", *q);
  1. A. Undefined behaviour: the cast is not allowed
  2. B. 7
  3. C. 9
  4. D. An address

Correct answer: B. 7

The standard guarantees there is no padding before the first member, and that a pointer to a struct, suitably converted, points to its first member. So q points at p.x and prints 7.

More C Programming questions

Exams that ask this topic