Question 8
Back to Exercise
What is the output of the following C program on a 64-bit system?
#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!