简体   繁体   中英

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.

#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.

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).

Moreover the array t declared like

char t[] = "";

is not enough large to store even one character gotten from scanf because it also need to store the terminating zero. Otherwise a call of scanf will have undefined behavior. And the call of scanf is also incorrect.

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

It should be written at least like

scanf("%s", t);

You could declare an object of the type char like

char t;

and then use scanf like

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 ;

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.

scanf("%10s", t);

char and int are data types, here the user only ever enters a string. Your question is really whether the user has entered somthing that may be interpreted as an integer or not.

isalpha() and isdigit() operate on single characters, but t is a string.

The following will check the first character of the string 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 .

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 ; 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.

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