gpt4 book ai didi

c - atoi 值的差异

转载 作者:行者123 更新时间:2023-12-02 05:59:57 26 4
gpt4 key购买 nike

我有以下代码:

char* input = (char*)malloc(sizeof(char) * BUFFER) // buffer is defined to 100
int digit = atoi(input); // convert char into a digit
int digit_check = 0;
digit_check += digit % 10; // get last value of digit

当我运行输入 1234567896 和类似的 digit = 1234567896 和 digit_check = 6 时。

但是,当我运行输入 9999999998 时,digit = 141006540​​6,因此 digit_check = 6,而它应该是 8。

对于第二个例子,为什么 input 和 digit 应该是相同的值却有不同?

最佳答案

可能是因为 9999999998 大于最大(有符号)整数表示,所以会溢出。

事实上这是 9999999998 和 141006540​​6 的二进制表示:

10  01010100 00001011 11100011 11111110
01010100 00001011 11100011 11111110

如你所见,如果你看到141006540​​6是9999999998的32位值

关于c - atoi 值的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29349076/

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