结构C中":"的含义是什么

Dav*_*ave 6 c struct

可能重复:
'unsigned temp:3'表示什么

  struct Test
  {
      unsigned a : 5;
      unsigned b : 2;
      unsigned c : 1;
      unsigned d : 5;
  };

  Test B;
  printf("%u %u %u %u", B.a, B.b, B.c, B.d); // output: 0 0 0 0
  static struct   Test A = { 1, 2, 3, 4};
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下:struct的目的是什么,printf只是输出0所以我认为这些不是默认值,但它们是什么呢?

也有人可以解释我为什么A.a, A.b, A.c, A.d输出1, 2, 1, 4而不是1, 2, 3, 4

Ric*_*III 11

那是一个有点领域.

它基本上告诉编译器hey, this variable only needs to be x bits wide, so pack the rest of the fields in accordingly, OK