C- Size of structure when bit field is used and how it is stored in memory

后端 未结 3 969
后悔当初
后悔当初 2021-01-06 04:13
void main()
{
  struct bitfield
  {
    signed int a :3;
    unsigned int b :13;
    unsigned int c :1;
  };

  struct bitfield bit1 = { 2, 14, 1 };
  clrscr();
  pr         


        
3条回答
  •  花落未央
    2021-01-06 04:30

    How bit-field is stored in memory is implementation dependent.

    One possible reason is, the bit-field which has 17 bits, should hold at least 3 bytes, but the compiler choose to pad it to 4 bytes.

    Again, almost everything about bit-field is implementation dependent, including the size of them and memory layout.

    Last, don't use void main, always stay with int main

提交回复
热议问题