Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the colon do in this struct definition? [duplicate]

Tags:

c

Possible Duplicate:
What does 'unsigned temp:3' means

I don't understand this struct definition. It seems illegal to me, but apparently it isn't:

typedef struct
{
    unsigned i:1;
} my_struct;

I believe that marking the variable as unsigned without a type is the same as marking it as unsigned int. However, I'm completely baffled by the colon. What does it do? My guess is that it's some sort of initializer, but I haven't a clue what it means. Off the top of my head, the only place that I'd expect a colon in C is in the ternary operator, which this obviously isn't. So, this appears to be a construct with which I am not familiar. Can anyone explain what the purpose of the colon is in this struct definition? What exactly does the declaration unsigned i:1; mean?

like image 982
Jonathan M Davis Avatar asked Dec 07 '10 06:12

Jonathan M Davis


People also ask

What does colon mean in struct?

Basically, the number after the colon describes how many bits that field uses.

What Does a colon do in C?

Many languages including C and Java use the colon to indicate the text before it is a label, such as a target for a goto or an introduction to a case in a switch statement.


1 Answers

It defines i to be of 1 bit width. If i:x is given then it defines i to be x bits wide.

like image 101
Chaithra Avatar answered Sep 20 '22 16:09

Chaithra