这在c int a中是什么意思:16;?
可能重复:
‘unsigned temp:3’是什么意思?
请注意这个符号是什么意思
int a:16;
我发现它是这样的代码,它确实编译。
struct name {int a:16; }
这是一个位域 。
这个特定的位域没有多大意义,因为你可以使用16位类型,并且你会浪费一些空间,因为位域被填充到int
的大小。
通常,您将它用于包含位大小元素的结构:
struct { unsigned nibble1 : 4; unsigned nibble2 : 4; }
struct name { int a:16; }
这意味着a
被定义为16位存储空间。 来自int
的其余位(16位)可用于定义另一个变量,例如b
,如下所示:
struct name { int a:16; int b:16; }
因此,如果int
是32位(4字节),那么一个int
的内存被分成两个变量a
和b
。
PS:我假设sizeof(int)
= 4个字节,1个字节= 8位
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.*/