1. Home
  2. Computer Science MCQs
  3. C Programming
  4. struct F { unsigned flag : 3; }; is declared. What range…

Added 30 Nov -0001

struct F { unsigned flag : 3; }; is declared. What range can flag hold, and what happens when 8 is assigned?

  1. -4 to 3, because a three-bit field is treated as signed unless unsigned is repeated
  2. 0 to 7, with 8 stored as 0 because the value is kept modulo 8 by the field Correct answer
  3. 0 to 7, with 8 raising a run-time overflow trap on conforming implementations
  4. 0 to 255, because bit-fields are always widened to a full byte when stored

Correct answer

B. 0 to 7, with 8 stored as 0 because the value is kept modulo 8 by the field

17 views

MCQSEXAM

Explanation

Bit-fields of n unsigned bits hold 0 to 2^n - 1, so a three-bit field holds 0 to 7 and assigning 8 keeps the low three bits, giving 0. A plain int bit-field is implementation-defined as signed or unsigned, which is why unsigned should be written explicitly. Field ordering and packing are also implementation-defined, so bit-fields must not be used for binary interchange formats.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs