簡體   English   中英

isdigit()異常行為?

[英]isdigit() unexpected behaviour?

為什么isdigit無法按預期工作。 我正在嘗試檢查輸入是否為數字。 如果輸入為數字,則輸出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");
}

但是它總是返回False

輸入:

高度:富

因為您的期望不正確。 在字符上調用isdigit()以查看其是否包含十進制數字'0'-'9'。 您正在一個已經是二進制值的int上調用它。

您對函數isdigit理解不正確。 該函數的輸入為char。 因此您可以更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM