gpt4 book ai didi

c - 我们如何得到以下输出?

转载 作者:行者123 更新时间:2023-12-01 16:19:13 25 4
gpt4 key购买 nike

#include <stdio.h>

int main(void)
{
int i = 258;
char ch = i;
printf("%d", ch)
}

输出是2!

变量的范围如何工作? c语言中不同数据类型的范围是多少?

最佳答案

当分配给较小的类型时,该值为

  • 截断,即258 % 256(如果新类型无符号)
  • 如果新类型已签名,则以实现定义的方式进行修改

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.

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.

所以所有那些花哨的“加法或减法”意味着它的分配就像你说的:

ch = i % 256;

关于c - 我们如何得到以下输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11708910/

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