printf("Enter a number or type 'Exit' to exit\n");
long val = 0;
int y = 3;
scanf("%s", input);
val = atoi(input);
if (val < y) {
printf("Hey You!");
}
else {
printf("Error, no such cell exists.\n");
}
当用户输入“0”、“1”或“2”时,会打印“Hey You”。但是,当用户键入任何字母(例如“y”或“k”)时,也会打印“Hey You”。如何确保仅在低于 3(即 y)的值时打印“Hey You”。
最佳答案
来自 C 标准
If the value of the result cannot be represented, the behaviour is undefined.
可以返回任何值。在您的情况下,该值低于 3。但不能保证。
atoi
如果无法执行转换,则不返回任何特殊值或不设置任何错误变量
关于c - Atoi 函数无法正确转换字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60132256/