gpt4 book ai didi

c - C中无符号到有符号的转换

转载 作者:太空狗 更新时间:2023-10-29 16:02:24 24 4
gpt4 key购买 nike

以下是否保证工作或实现已定义?

unsigned int a = 4294967294;
signed int b = a;

b 的值在 gcc 上是 -2

From C99 (§6.3.1.3/3) Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

最佳答案

a 值到 signed int 的转换是实现定义的(正如您正确提到的那样,因为 6.3.1.3p3)。例如,在某些系统上,它可以是 INT_MAX(饱和转换)。

gcc 的实现行为定义如下:

The result of, or the signal raised by, converting an integer to a signed integer type when the value cannot be represented in an object of that type (C90 6.2.1.2, C99 6.3.1.3).

For conversion to a type of width N, the value is reduced modulo 2^N to be within range of the type; no signal is raised.

http://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html

关于c - C中无符号到有符号的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13636397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com