简体   繁体   English

如何在 c 中检查 int 或 char

[英]How to check int or char in c

I have a code where I convert celsius to fahrenheit.我有一个将摄氏度转换为华氏度的代码。 And I need to check what user writes char or int.我需要检查用户写的是什么字符或整数。

I've tried isalpha and isdigit, but they do not work.我尝试过 isalpha 和 isdigit,但它们不起作用。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
    char t[] = "";
    scanf("%s", &t);
    if(isalpha(t))
    {
        printf("It's char\n");
    }
    else if (isdigit(t))
    {
        printf("It's int\n");
    }


    return 0;
}

isalpha and isdigit are applied to objects of the type int that contain a char. isalphaisdigit应用于包含 char 的 int 类型的对象。

You are trying to apply these functions to an object of the type char * (an array type is implicitly converted to a pointer type in expressions).您正在尝试将这些函数应用于 char * 类型的 object(数组类型在表达式中隐式转换为指针类型)。

Moreover the array t declared like此外,数组t声明为

char t[] = "";

is not enough large to store even one character gotten from scanf because it also need to store the terminating zero.甚至不足以存储从 scanf 获得的一个字符,因为它还需要存储终止零。 Otherwise a call of scanf will have undefined behavior.否则 scanf 的调用将具有未定义的行为。 And the call of scanf is also incorrect.而且scanf的调用也不正确。

scanf("%s", &t);
            ^^^

It should be written at least like它至少应该写成

scanf("%s", t);

You could declare an object of the type char like你可以声明一个 char 类型的 object

char t;

and then use scanf like然后像使用 scanf

scanf(" %c", &t);

and at last最后

if ( isalpha( ( unsigned char )t ) )
{
    printf("It's char\n");
}
else if ( isdigit( ( unsigned char )t ) )
{
    printf("It's int\n");
}

Firstly:首先:

char t[] = "";

creates a buffer of exactly one character, then创建一个只有一个字符的缓冲区,然后

scanf("%s", t);

will overrun that buffer for anything but an empty string input.除了空字符串输入之外的任何内容都会超出该缓冲区。 Making scanf() safe from overrun is not straightforward, but even then most naive implementation will have a practical buffer length eg ;使scanf()避免溢出并不简单,但即使如此,大多数天真的实现也会有一个实用的缓冲区长度,例如;

char t[128] = "" ;

If the expectation is to enter a string that can be converted to an int , then 10 decimal digits is sufficient for all positive 32bit integers.如果期望输入一个可以转换为int的字符串,那么 10 个十进制数字对于所有 32 位整数来说就足够了。

scanf("%10s", t);

char and int are data types, here the user only ever enters a string. charint是数据类型,这里用户只输入一个字符串。 Your question is really whether the user has entered somthing that may be interpreted as an integer or not.您的问题实际上是用户是否输入了可能被解释为 integer 的内容。

isalpha() and isdigit() operate on single characters, but t is a string. isalpha()isdigit()对单个字符进行操作,但t是一个字符串。

The following will check the first character of the string t :以下将检查字符串t的第一个字符:

if( isdigit(t[0]) )
{
    printf("It's digit\n");
}
else
{
    printf("It's not a digit\n");
}

Note that it makes little sense testing for isalpha() because the union of all digits + all alpha, is still only a subset of all characters .请注意,对isalpha()进行测试毫无意义,因为所有数字 + 所有 alpha 的并集仍然只是所有字符的子集。

If in fact you simply wish to verify that the entire string is numeric then:如果实际上您只是想验证整个字符串是否为数字,那么:

for( int i = 0; t[i] != 0 && isdigit(t[i]) i++ )
{
    // nothing
} 

if( t[i] == 0 )
{
     printf("It's numeric\n");
}
else
{
     printf("It's not entirely numeric\n");
}

Even then it is not a given that the numeric string can be represented by an int ;即使这样,数字字符串也不能用int表示; it also has to be in range.它也必须在范围内。 You might also want to consider the possibility of a -/+ sign at the start, and might consider ignoring trailing non-numeric digits.您可能还想考虑在开头使用 -/+ 符号的可能性,并可能考虑忽略尾随的非数字数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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