为什么Isdigit功能不能正常工作?

时间:2022-05-16 20:08:06

I wrote this short code to test my understanding of the isdigit function:

我写了这个简短的代码来测试我对isdigit函数的理解:

int inChar;
printf("enter input:");
scanf(" %d", &inChar);

if (isdigit(inChar))
   printf("Your input was a number"); 
else
   printf("Your input was not a number.\n");

When I test this program and I enter a number, C returns the else statement (Your input was not a number.). So regardless of if I enter a number or a letter, the program returns the else statement.

当我测试这个程序并输入一个数字时,C返回else语句(你的输入不是数字。)。因此无论我输入数字还是字母,程序都会返回else语句。

Why is this so?

为什么会这样?

4 个解决方案

#1


2  

isdigit() checks if a single character that was passed to it by converting the char value an unsigned char. So, you can't directly pass any int value and expect it to work.

isdigit()通过将char值转换为unsigned char来检查传递给它的单个字符。因此,您不能直接传递任何int值并期望它工作。

Man isdigit() says:

Man isdigit()说:

   isdigit()
          checks for a digit (0 through 9).

To check single digit, you can modify:

要检查单个数字,您可以修改:

char inChar;
printf("enter input:");
scanf(" %c", &inChar);

if (isdigit((unsigned char)inChar)) {
   printf("Your input was a number"); 
}
else {
printf("Your input was not a number.\n");
}

If you have an array (a string containing a number) then you can use a loop.

如果你有一个数组(一个包含数字的字符串),那么你可以使用一个循环。

#2


2  

The function's purpose is to classify characters (like '3'). Running it on something that's read using %d doesn't make sense.

该函数的目的是对字符进行分类(如“3”)。在使用%d读取的内容上运行它没有意义。

You should read a single char using %c. Remember to check that reading succeeded.

您应该使用%c读取一个字符。记得检查读数是否成功。

#3


1  

The C library function void isdigit(int c) checks if the passed character is a decimal digit character.

C库函数void isdigit(int c)检查传递的字符是否为十进制数字字符。

If you badly wanna try it with an int you can init in this way

如果你非常想用int尝试它,你可以用这种方式初始化

int inChar = '2';

The following code gave expected results.

以下代码给出了预期的结果。

int main()
{
    char inChar;
    printf("enter input:");
    scanf(" %c", &inChar);
    if (isdigit(inChar))
        printf("Your input was a number. \n");
    else
        printf("Your input was not a number.\n");
    return 0;   
}

Output:

vinay-1> ./a.out
enter input:1
Your input was a number

#4


0  

Even though isdigit has the format

即使isdigit有格式

int isdigit(int c);  // 7.4.1.5

it actually expects a character. As in: a symbol table value.

它实际上需要一个角色。如:符号表值。

If you read an int with scanf("%d") then you get the raw integer value, for example 1. But isdigit would need the symbol value '1', which is the raw value 49 in most symbol tables. The whole purpose of isdigit is to check if the input is an ASCII digit.

如果使用scanf(“%d”)读取int,则获得原始整数值,例如1.但isdigit需要符号值“1”,这是大多数符号表中的原始值49。 isdigit的全部目的是检查输入是否为ASCII数字。

To fix the problem, preferably use character types. Or alternatively, pass isdigit(inChar+'0') to convert the raw value to a symbol table value.

要解决此问题,最好使用字符类型。或者,传递isdigit(inChar +'0')将原始值转换为符号表值。

#1


2  

isdigit() checks if a single character that was passed to it by converting the char value an unsigned char. So, you can't directly pass any int value and expect it to work.

isdigit()通过将char值转换为unsigned char来检查传递给它的单个字符。因此,您不能直接传递任何int值并期望它工作。

Man isdigit() says:

Man isdigit()说:

   isdigit()
          checks for a digit (0 through 9).

To check single digit, you can modify:

要检查单个数字,您可以修改:

char inChar;
printf("enter input:");
scanf(" %c", &inChar);

if (isdigit((unsigned char)inChar)) {
   printf("Your input was a number"); 
}
else {
printf("Your input was not a number.\n");
}

If you have an array (a string containing a number) then you can use a loop.

如果你有一个数组(一个包含数字的字符串),那么你可以使用一个循环。

#2


2  

The function's purpose is to classify characters (like '3'). Running it on something that's read using %d doesn't make sense.

该函数的目的是对字符进行分类(如“3”)。在使用%d读取的内容上运行它没有意义。

You should read a single char using %c. Remember to check that reading succeeded.

您应该使用%c读取一个字符。记得检查读数是否成功。

#3


1  

The C library function void isdigit(int c) checks if the passed character is a decimal digit character.

C库函数void isdigit(int c)检查传递的字符是否为十进制数字字符。

If you badly wanna try it with an int you can init in this way

如果你非常想用int尝试它,你可以用这种方式初始化

int inChar = '2';

The following code gave expected results.

以下代码给出了预期的结果。

int main()
{
    char inChar;
    printf("enter input:");
    scanf(" %c", &inChar);
    if (isdigit(inChar))
        printf("Your input was a number. \n");
    else
        printf("Your input was not a number.\n");
    return 0;   
}

Output:

vinay-1> ./a.out
enter input:1
Your input was a number

#4


0  

Even though isdigit has the format

即使isdigit有格式

int isdigit(int c);  // 7.4.1.5

it actually expects a character. As in: a symbol table value.

它实际上需要一个角色。如:符号表值。

If you read an int with scanf("%d") then you get the raw integer value, for example 1. But isdigit would need the symbol value '1', which is the raw value 49 in most symbol tables. The whole purpose of isdigit is to check if the input is an ASCII digit.

如果使用scanf(“%d”)读取int,则获得原始整数值,例如1.但isdigit需要符号值“1”,这是大多数符号表中的原始值49。 isdigit的全部目的是检查输入是否为ASCII数字。

To fix the problem, preferably use character types. Or alternatively, pass isdigit(inChar+'0') to convert the raw value to a symbol table value.

要解决此问题,最好使用字符类型。或者,传递isdigit(inChar +'0')将原始值转换为符号表值。