#include <stdio.h>
int main(int argc, char** argv) {
int num = 0;
printf("Input: ");
scanf("%d", &num); <<<
printf("%d\n", num);
return 0;
}
扫描(“%d”,&编号);
- 铿锵整齐:“scanf”用于将字符串转换为整数值,但函数不会报告转换错误;考虑使用“strtol”代替 *
我用CLion写了一个非常简单的代码,它建议我使用“strtol”而不是“scanf”。
但是我只使用整型变量,没有字符串,我不明白为什么会弹出检查消息。
如何修改此代码?
1条答案
按热度按时间6tdlim6h1#
如何修改此代码?
如果成功,
strtol()
返回转换后的long int值。如果不成功,则
strtol()
在无法执行转换的情况下返回0
。如果正确的值在可表示值的范围之外,则strtol()
根据值的符号返回LONG_MAX
或LONG_MIN
。如果不支持base的值,则strtol()
返回0
。如果不成功,
strtol()
将errno设置为以下值之一:EINVAL不支持base的值。
ERANGE转换导致溢出。源:IBM
例如,您可以使用
scanf()
检查溢出吗?也许离题了,但是Jonathan Leffler在他的评论(在另一个主题中)中说,* 把警告当作错误来处理。*