这在c int a中是什么意思:16;?

时间:2011-01-16 16:34:01

标签: c++ c programming-languages annotations

  

可能重复:
  What does 'unsigned temp:3' mean?

请注意这个符号是什么意思

  

int a:16;

我发现它是这样的代码,它确实可以编译。

  

struct name {        int a:16; }

4 个答案:

答案 0 :(得分:16)

这是bitfield

这个特定的位域没有多大意义,因为你只能使用一个16位类型,并且你会浪费一些空间,因为位域的填充大小为int

通常,您将它用于包含位大小元素的结构:

struct {
    unsigned nibble1 : 4;
    unsigned nibble2 : 4;
}

答案 1 :(得分:11)

struct name { int a:16; }

这意味着a被定义为 16位内存空间。来自int的其余位(16位)可用于定义另一个变量,例如b,如下所示:

struct name { int a:16;  int b:16; }

因此,如果int是32位(4字节),那么一个int的内存将分为两个变量ab

PS:我假设sizeof(int) = 4个字节,1个字节= 8位

答案 2 :(得分:2)

 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.*/

答案 3 :(得分:0)

这是一个位域。

我从未见过16位的位域;通常是短暂的。

http://www.cs.cf.ac.uk/Dave/C/node13.html