C - Character

Example 1: Program to declare and display character data type.

void main()
{
   char c='A', d=65;
   clrscr();
   printf("%c\n", c);
   printf("%c\n", d);
   getch();
}
A
A

Types of Functions

There are two types of functions in C-Language:

  1. Library Functions: are the pre-defined functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), clrscr(), getch() etc.
  2. User-defined functions: are the functions which are created/defined by the programmers.

Library Functions - Input Character

Functions to input a single character are available in 'stdio.h' header file. These functions are:

  1. getch(): 'get' stands for input, 'ch' stands for character. Function getch() is use to input single character, but it will not display the character entered by the user.
    Syntax: char a = getch();
  2. getche(): getche() is same as getch() function only the difference is that it will display the character entered by the user. 'get' stands for input, 'ch' stands for character, and 'e' stands for echo(display)
    Syntax: char a = getche();
    Both getch() and getche() functions will not require enter key to terminate.
  3. getchar(): It will terminate by enter key after inputting a single character.
    Syntax: char a = getchar();

Example 2: Input Character and display it.

#include<stdio.h>
#include<conio.h>
void main()
{
    char a;
    clrscr();
    printf("Enter character: ");
    a = getch();
    //a = getche();
    //a = getchar();
    
    printf("\nYou have pressed: %c",a);
    //putch(a);
    //putchar(a); //Output of putch and putchar is same
    getch();
}
Enter character:
You have pressed: a

Example 3: Program to explain difference between getch() and getche() function.

void main()
{
   char ch;
   clrscr();	
   printf("Enter character: ");
   ch=getch();
   printf("\nch=%c",ch);
   printf("\nEnter character: ");
   ch=getche();
   printf("\nch=%c",ch);
   getch();
}
Enter character:
ch=a
Enter character: a
ch=a

Character output functions

  1. putch(): use to print a single character.
    Syntax: putch(a);
  2. putchar(): use to print a single character.
    Syntax: putchar(a);

C - Character Functions

C Character functions are used to identify whether the input character is alphabet, digit, lowercase, uppercase etc.

Built-in C - Character functions in ctype.h are:
S.NoCharacter FunctionDescription
isalpha()Return true if character is an alphabet or not.
isdigit()Return true if character is a digit or not.
isalnum()Return true if character is an alphabet, number or not.
islower()Return true if character is in lowercase or not.
isupper()Return true if character is in uppercase or not.
isspace()Return true if character is a space or not.
ispunct()Return true if character is punctuation or not.

Example 4: WAP to check whether the given character is Alpha-numeric or not.

void main()
{
   char ch;
   clrscr();	
   printf("Enter character: ");
   ch=getche();
   if(isalnum(ch))
      printf("\nIt is alpha-numeric character.");
   else
      printf("\nIt is not an alpha-numeric character.");
   getch();
}
Output 1:
Enter character: A
It is alpha-numeric character.

Output 2:
Enter character: @
It is not an alpha-numeric character.

Example 5: WAP to check whether the given character is Alphabet or not.

void main()
{
   char ch;
   clrscr();	
   printf("Enter character: ");
   ch=getche();
   if(isalpha(ch))
      printf("\nIt is a alphabet character.");
   else
      printf("\nIt is not an alphabet character.");
   getch();
}
Output 1:
Enter character: A
It is alphabet character.

Output 2:
Enter character: 2
It is not an alphabet character.