gpt4 book ai didi

c - 无符号整型算术 - C 初学者

转载 作者:行者123 更新时间:2023-11-30 18:03:37 25 4
gpt4 key购买 nike

冒着因发布愚蠢问题而被骂的风险:

假设我有以下代码片段:

unsigned int limit = 4294967296;
int math = 50000;

while(limit > 50000)
{
limit = limit - math;
printf("new limit is - \t%u\n", limit);
}

在 unsigned int 类型和 int 类型之间进行算术运算会对内存使用产生影响吗?

我知道 unsigned int 的内存要多一个数量级(int 为 2^31,unsigned int 为 2^32),但是,由于我使用 unsigned int 作为占位符,我想我消除了溢出的危险。

我会自己运行这个,但我现在没有可访问的 Linux 盒子。

对此的任何意见将不胜感激。

最佳答案

这将永远循环,因为您最终将得到环绕(即 limit 永远不会小于 0)...这是您的问题吗?

关于c - 无符号整型算术 - C 初学者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8184686/

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