【发布时间】:2020-06-30 16:38:39
【问题描述】:
我是一个完整的 cs50 课程初学者,我需要检查用户的参数是否为数字。
这是代码:
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
int main(void)
{
int i = 1;
if (isdigit(i) == 1)
{
printf("i is a digit");
}
else if (isdigit(i) == 0)
{
printf("i is not a digit");
}
return 0;
}
当我运行这段代码时,我基本上得到了我应该得到的相反结果。当 i 是一个数字时,它会打印出“i is not a number”,反之亦然。 我究竟做错了什么?我认为 isdigit 如果是数字则返回非零值,否则返回 0。基本上1为真,0为假。这不正确吗? 非常感谢,谢谢!
【问题讨论】:
-
也许一些文档会有所帮助:
isdigit