结构 - 解释输出: [英] Struct - Explain the output:

查看:36
本文介绍了结构 - 解释输出:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 C 代码.

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); 
}

当我执行代码时,我得到以下输出:

When I execute the code, I am getting the following output:

2 2 1.

但是输出应该是2 -6 5,对吧?

But output should be 2 -6 5, right?

如果我错了,请解释.

推荐答案

-6 超出了 3 位有符号整数的范围.因此,您正在观察未定义 实现定义的行为(实际上,您的价值中最重要的部分被丢弃了).

-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).

这篇关于结构 - 解释输出:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆