Sunday, July 12, 2009

C code question (about variable assignment)?

Be given the code below:





#include%26lt;stdio.h%26gt;


struct test{


unsigned a:3, b, c;


};


void main(){


struct test x;


x.a = x.b = x.c = 10;


printf("%d %d %d ", x.a, x.b, x.c);


x.c = x.a = x.b = 10;


printf("%d %d %d", x.a, x.b, x.c);


}





/*


PRINTS:


2 10 10 2 10 2


*/





After laborious Google searches, still couldn't find exactly what the ":" in "a:3" struct declaration does. Anyone have an idea?

C code question (about variable assignment)?
It's for so-called "bit fields".





:: unsigned a:3;


basically means that variable 'a' will be three bits wide.





It's almost as evil as goto (because its completely unportable), so you shouldn't use it for real software.


No comments:

Post a Comment