[英]isdigit() unexpected behaviour?
Why is the isdigit
not working as expected. 为什么
isdigit
无法按预期工作。 i am trying to check if the input is a digit or not. 我正在尝试检查输入是否为数字。 If the input is digit, then print
True
else False
. 如果输入为数字,则输出
True
否则为False
。
#include<stdio.h>
#include<ctype.h>
int main()
{
int h;
printf("Height: ");
scanf("%d", &h);
if (isdigit(h))
printf("True");
else
printf("False");
}
But it returns False
always. 但是它总是返回
False
。
Input: 输入:
Height: Foo 高度:富
Because your expectations are incorrect. 因为您的期望不正确。 isdigit() is called on a character to see if it contains a decimal digit, '0'-'9'.
在字符上调用isdigit()以查看其是否包含十进制数字'0'-'9'。 You are calling it on an int which is already a binary value.
您正在一个已经是二进制值的int上调用它。
You have wrong understand about function isdigit
. 您对函数
isdigit
理解不正确。 the input of this function is char. 该函数的输入为char。 so you can change to this:
因此您可以更改为:
char h;
printf("Height: ");
scanf("%c", &h);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.