66

How do I determine if a char in C such as a or 9 is a number or a letter?

Is it better to use:

int a = Asc(theChar);

or this?

int a = (int)theChar
abhi
  • 1,760
  • 1
  • 24
  • 40
Mona
  • 1,043
  • 2
  • 12
  • 21

7 Answers7

120

You'll want to use the isalpha() and isdigit() standard functions in <ctype.h>.

char c = 'a'; // or whatever

if (isalpha(c)) {
    puts("it's a letter");
} else if (isdigit(c)) {
    puts("it's a digit");
} else {
    puts("something else?");
}
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
37

chars are just integers, so you can actually do a straight comparison of your character against literals:

if( c >= '0' && c <= '9' ){

This applies to all characters. See your ascii table.

ctype.h also provides functions to do this for you.

Christopher Neylan
  • 8,018
  • 3
  • 38
  • 51
14

<ctype.h> includes a range of functions for determining if a char represents a letter or a number, such as isalpha, isdigit and isalnum.

The reason why int a = (int)theChar won't do what you want is because a will simply hold the integer value that represents a specific character. For example the ASCII number for '9' is 57, and for 'a' it's 97.

Also for ASCII:

  • Numeric - if (theChar >= '0' && theChar <= '9')
  • Alphabetic -
    if (theChar >= 'A' && theChar <= 'Z' || theChar >= 'a' && theChar <= 'z')

Take a look at an ASCII table to see for yourself.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
  • 2
    The check for '0' <= x <= '9' is correct for all standard-compliant implementations (ASCII or not is irreverent). C requires that decimal digits have a contiguous representation. As for letters, all bets are off. – Wiz Jan 22 '15 at 00:50
10

Neither of these does anything useful. Use isalpha() or isdigit() from the standard library. They're in <ctype.h>.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
7

If (theChar >= '0' && theChar <='9') it's a digit. You get the idea.

Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
6

You can normally check for ASCII letters or numbers using simple conditions

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
    /*This is an alphabet*/
}

For digits you can use

if (ch >= '0' && ch <= '9')
{
    /*It is a digit*/
}

But since characters in C are internally treated as ASCII values you can also use ASCII values to check the same.

How to check if a character is number or letter

plasmacel
  • 8,183
  • 7
  • 53
  • 101
Pankaj Prakash
  • 2,300
  • 30
  • 31
  • as every intermediate c text mentions, it is not guaranteed that c uses ascii so this wont work "everywhere" – Ankur S Jul 22 '18 at 06:34
3

C99 standard on c >= '0' && c <= '9'

c >= '0' && c <= '9' (mentioned in another answer) works because C99 N1256 standard draft 5.2.1 "Character sets" says:

In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

ASCII is not guaranteed however.

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985