I ran into the following code in an old exam of the C course I’m taking:
struct QuestionSet{
char q1:1;
char q2:1;
char q3:1;
}
I have no idea what the syntax “char q1:1” means, and I haven’t been able to find it anywhere in “The C Programming Language” which is the course book. Can anyone explain?
It’s a bitfield. The number after the colon indicates the number of bits to assign to the struct element. So the three elements are all one bit wide, and are able to store two values: 0, and either 1 or -1 (depending on your compiler, though -1 would be the more logical option when considering two’s-complement arithmetic).