C:简单算术输出的确认

发布于 2024-12-13 04:48:46 字数 88 浏览 6 评论 0原文

我能否确认以下 C 代码给出 b 的未定义结果:

unsigned a = 0;
int b = a - 1;

Can I confirm that the following C code gives undefined result for b:

unsigned a = 0;
int b = a - 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落墨 2024-12-20 04:48:46

(§6.3.1.3/3):“...结果是实现定义的,或者引发实现定义的信号。”

在典型的二进制补码硬件上,我期望结果为 -1。

请注意,结果是实现定义的,而不是未定义的。

(§6.3.1.3/3): "...either the result is implementation-defined or an implementation-defined signal is raised."

On typical twos-complement hardware, I'd expect the result to be -1.

Note that the result is implementation defined, not undefined.

握住我的手 2024-12-20 04:48:46

当您将 unsigned 分配给 int 时,会发生隐式类型转换。所以,它不是未定义的行为。

Implicit type conversion takes place when you assigned unsigned to int. So, its not undefined behavior.

北城孤痞 2024-12-20 04:48:46

它才会始终给出值

 a=0,b=-1  

仅当您想在在线 c 编译器 中查看时,

It always giving the values

 a=0,b=-1  

only if you want to check it out in the online c compiler

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文