what does this mean in c int a:16;? [duplicate]
Possible Duplicate:
What does 'unsigned temp:3' mean?
please what does this notation mean
int a:16;
I found it is code like this and it does compile.
struct name { int a:16; }
This is a bitfield.
This particular bitfield doesn't make much sense as you just could use a 16-bit type and you're wasting some space as the bitfield is padded to the size of int
.
Usually, you are using it for structures that contain elements of bit size:
struct {
unsigned nibble1 : 4;
unsigned nibble2 : 4;
}
struct name { int a:16; }
It means a
is defined as 16-bit memory space. The remaining bits (16 bits) from int
can be used to defined another variable, say b
, like this:
struct name { int a:16; int b:16; }
So if int
is 32-bit (4 bytes), then the memory of one int
is divided into two variables a
and b
.
PS: I'm assuming sizeof(int)
= 4 bytes, and 1 byte = 8 bits
struct s
{
int a:1;
int b:2;
int c:7;
};/*size of structure s is 4 bytes and not 4*3=12 bytes since all share the same space provided by int declaration for the first variable.*/
struct s1
{
char a:1;
};/*size of struct s1 is 1byte had it been having any more char _var:_val it would have been the same.*/
It's a bitfield.
I've never seen a 16 bit bitfield; usually that's a short.
http://www.cs.cf.ac.uk/Dave/C/node13.html
精彩评论