ProgrammingVery hardProduct company OA

What is printed?

What is printed?
struct { unsigned a : 3; unsigned b : 5; } f = {0, 0};
f.a = 9;
f.b = 33;
printf("%u %u ", f.a, f.b);
f.a += 7;
f.b = f.b - 2;
printf("%u %u", f.a, f.b);
  1. A. 1 1 0 31
  2. B. 9 33 16 31
  3. C. 1 1 0 0
  4. D. 1 1 8 31

Correct answer: A. 1 1 0 31

An unsigned bit-field of width w stores values modulo 2^w, exactly like an unsigned integer of that width. a has 3 bits: 9 mod 8 = 1, then 1 + 7 = 8 mod 8 = 0. b has 5 bits: 33 mod 32 = 1; f.b − 2 is computed in int as −1, and converting −1 to a 5-bit unsigned field gives 31. Nothing here is undefined: unsigned wrap-around is defined.

More C Programming questions

Exams that ask this topic