下面的代码给出了 2147483647 的输出。
如果if(atol(str)<=2147483647u
) 更改为 if(atol(str)<2147483647u)
,
输出为 100。
输入与 str= "2147483649"
相同.
#include <stdio.h>
#include <ctype.h>
int main()
{
unsigned long l = 100;
unsigned char str[19] = "2147483649";
if(atol(str)<=2147483647u)
{
l = atol(str);
}
printf("\n%ld",l);
return 0;
}
最佳答案
INT_MAX = 2147483647
atol()
返回一个 long
对于大于 int max 2147483647 的数字
改用atoll()
#include <stdlib.h>
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
关于c - 在 c 中用整数修复 u 导致 atol 发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16771298/