Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; }

like image 637
Ayoub M. Avatar asked Jan 16 '11 16:01

Ayoub M.


2 Answers

 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.*/
like image 108
vivek agarwal Avatar answered Oct 11 '22 20:10

vivek agarwal


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;
}
like image 39
schnaader Avatar answered Oct 11 '22 19:10

schnaader