Check if User Inputs a Letter or Number in C

Tater picture Tater · Sep 25, 2009 · Viewed 94.5k times · Source

Is there an easy way to call a C script to see if the user inputs a letter from the English alphabet? I'm thinking something like this:

if (variable == a - z) {printf("You entered a letter! You must enter a number!");} else (//do something}

I want to check to make sure the user does not enter a letter, but enters a number instead. Wondering if there is an easy way to pull every letter without manually typing in each letter of the alphabet :)

Answer

Brian R. Bondy picture Brian R. Bondy · Sep 25, 2009

It's best to test for decimal numeric digits themselves instead of letters. isdigit.

#include <ctype.h>

if(isdigit(variable))
{
  //valid input
}
else
{
  //invalid input
}