简体   繁体   中英

isdigit() unexpected behaviour?

Why is the isdigit not working as expected. i am trying to check if the input is a digit or not. If the input is digit, then print True else 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.

Input:

Height: Foo

Because your expectations are incorrect. isdigit() is called on a character to see if it contains a decimal digit, '0'-'9'. You are calling it on an int which is already a binary value.

You have wrong understand about function isdigit . the input of this function is char. so you can change to this:

char h;
printf("Height: ");
scanf("%c", &h);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM