LearnFrenzy
Sign in Take a free test

Structures, Unions, Enums · Predict the Output · Question 8

Storing 5 in a 2-bit bit-field – output Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What is the output of the following C program on a 64-bit system?

main.c
#include <stdio.h>

struct flags {
    unsigned mode : 2;
};

int main(void) {
    struct flags f = {0};
    f.mode = 5;
    printf("%u\n", f.mode);
    return 0;
}
  • A 5
  • B 3
  • C 0
  • D 1 Correct
Correct Answer: Option D

An unsigned 2-bit field holds 0 to 3. Assigning 5 (binary 101) keeps only the low 2 bits, 01, so it prints 1. Compilers warn about the truncation.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links