Struct - Explain the output:

前端 未结 3 2015
一向
一向 2021-01-20 21:53

I have the following C code.

struct values{
  int a:3;
  int b:3;
  int c:2;
};

void main(){
  struct values v={2,-6,5};
  printf(\"%d %d %d\",v.a,v.b,v.c);         


        
3条回答
  •  Happy的楠姐
    2021-01-20 22:32

    -6 exceeds the range of a 3-bit signed int. Therefore you're observing an artifact of undefined implementation-defined behaviour (in practice, the most-significant bits of your value are being thrown away).

提交回复
热议问题