What is the function of the colon : in c structure

w‮l.ww‬autturi.com
What is the function of the colon : in c structure

The role of the colon is to share the bits of bytes in order to save memory space.

int is usually 4 bits, but if we want to represent a state, only 0,1. we can use only 1 bit .

struct example {
  int state1 : 1;  
  int state2 : 2;
  int state3 : 1; 
  int state4 : 3; 
};

state1,state2,state3 are in the same int data.

Created Time:2017-08-28 06:04:46  Author:lautturi