gpt4 book ai didi

c - C 中的类型转换(降级)

转载 作者:行者123 更新时间:2023-11-30 21:36:31 29 4
gpt4 key购买 nike

有人可以解释一下吗:

When the destination is some form of unsigned integer and the assigned value is an integer, the extra bits that make the value too big are ignored.

我不明白“目标类型”和分配的值的含义。

以上是 C Primer Plus(我已完成 6 版)第 174 页 Ch5 类型转换部分的一段内容

最佳答案

“目标”是指被赋值的变量,即赋值的左侧,而“赋值”是指被赋值给变量的值,即赋值的右侧。

例如:

unsigned int destination = assigned_value;

至于当分配的值超出目标范围时会发生什么,C standard的第6.3.1.3节关于有符号和无符号整数的转换,说明如下:

1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.

2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

这实际上意味着该值将以目标可以容纳的最大值加 1 为模进行转换。换句话说,多余的位被去除。

关于c - C 中的类型转换(降级),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49775051/

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