CLion 建议使用 'strtol' 而不是 'scanf'
CLion recommends to use 'strtol' instead of 'scanf'
#include <stdio.h>
int main(int argc, char** argv) {
int num = 0;
printf("Input: ");
scanf("%d", &num); <<<
printf("%d\n", num);
return 0;
}
scanf("%d", &num);
Clang-Tidy:'scanf'用于将字符串转换为整数值,但函数不会报转换错误;考虑使用 'strtol' 而不是
我用 CLion 写了一个非常简单的代码,它推荐我 'strtol' 而不是 'scanf'。
但我只使用整数变量,没有字符串。想不通为什么会弹出检查信息
如何修改此代码?
How do I modify this code?
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
enum { INPUT_SIZE = 30 };
int main () {
char *ptr;
long ret;
char str[INPUT_SIZE];
fgets(str, INPUT_SIZE, stdin);
ret = strtol(str, &ptr, 10);
if( ret == LONG_MAX || ret == LONG_MIN ) {
perror("!! Problem is -> ");
}
else if (ret) {
printf("The number is %ld\n", ret);
}
else {
printf("No number found input is -> %s\n", ptr);
}
return(0);
}
If successful, strtol()
returns the converted long int value.
If unsuccessful, strtol()
returns 0
if no conversion could be
performed. If the correct value is outside the range of representable
values, strtol()
returns LONG_MAX
or LONG_MIN
, according to the sign
of the value. If the value of base is not supported, strtol()
returns 0
.
If unsuccessful strtol()
sets errno to one of the following values:
Error Codes:
EINVAL The value of base is not supported.
ERANGE The conversion caused an overflow.
Source : IBM
你可以使用 scanf()
来检查溢出吗?
Input: 1234 Whosebug
Output: The number is 1234
Input: 123nowhitespace
Output: The number is 123
Input: number is 123
Output: No number found input is -> number is 123
Input: between123between
Output: No number found input is -> between23between
Input: 9999999999999999999
Output: !! Problem is -> : Result too large
可能跑题了,但是,Jonathan Leffler 在他的评论(在另一个主题中)中说 像处理错误一样处理警告。
#include <stdio.h>
int main(int argc, char** argv) {
int num = 0;
printf("Input: ");
scanf("%d", &num); <<<
printf("%d\n", num);
return 0;
}
scanf("%d", &num);
Clang-Tidy:'scanf'用于将字符串转换为整数值,但函数不会报转换错误;考虑使用 'strtol' 而不是
我用 CLion 写了一个非常简单的代码,它推荐我 'strtol' 而不是 'scanf'。
但我只使用整数变量,没有字符串。想不通为什么会弹出检查信息
如何修改此代码?
How do I modify this code?
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
enum { INPUT_SIZE = 30 };
int main () {
char *ptr;
long ret;
char str[INPUT_SIZE];
fgets(str, INPUT_SIZE, stdin);
ret = strtol(str, &ptr, 10);
if( ret == LONG_MAX || ret == LONG_MIN ) {
perror("!! Problem is -> ");
}
else if (ret) {
printf("The number is %ld\n", ret);
}
else {
printf("No number found input is -> %s\n", ptr);
}
return(0);
}
If successful,
strtol()
returns the converted long int value.If unsuccessful,
strtol()
returns0
if no conversion could be performed. If the correct value is outside the range of representable values,strtol()
returnsLONG_MAX
orLONG_MIN
, according to the sign of the value. If the value of base is not supported,strtol()
returns0
.If unsuccessful
strtol()
sets errno to one of the following values:Error Codes:
EINVAL The value of base is not supported.
ERANGE The conversion caused an overflow. Source : IBM
你可以使用 scanf()
来检查溢出吗?
Input: 1234 Whosebug
Output: The number is 1234
Input: 123nowhitespace
Output: The number is 123
Input: number is 123
Output: No number found input is -> number is 123
Input: between123between
Output: No number found input is -> between23between
Input: 9999999999999999999
Output: !! Problem is -> : Result too large
可能跑题了,但是,Jonathan Leffler 在他的评论(在另一个主题中)中说 像处理错误一样处理警告。