C Program to Find Total of Even Integers

C Program for Testing Character Type Use of getchar Function

In this Example, gives How to write Testing Character Type Use of getchar Function with an example.

The program of below requests the user to enter a character and displays a message on the screen telling the user whether the character is an alphabet or digit, or any other special character.

This program receives a character from the keyboard and tests whether it is a letter or digit and prints out a message accordingly. These tests are done with the help of the following functions:

  1. isalpha(character)
  2. isdigit(character)

For example, isalpha assumes a value non-zero (TRUE) if the argument character contains an alphabet; otherwise it assumes 0 (FALSE). Similar is the case with the function isdigit.

Testing Character Type Use of getchar Function

#include <stdio.h>
#include <ctype.h>
main()
{
       char character;
       printf("Press any key\n");
       character = getchar();
   if (isalpha(character) > 0)
   printf("The character is a letter.");
else
   if (isdigit (character) > 0)
   printf("The character is a digit.");
else
   printf("The character is not alphanumeric.");
}

Output:

Press any key
h
The character is a letter.
Press any key
5
The character is a digit.
Press any key
*
The character is not alphanumeric.

Let me know if you find any difficulty in understanding this example and I would be glad to explain it further.