getchar() Library Functions with Examples

getchar() is also a character input functions. It reads a character and accepts the input until carriage return is entered (i.e. until enter is pressed). It is defined in header file stdio.h.

getchar() Syntax


character_variable = getchar();

getchar() Examples

Example #1 : Reading Character Using getchar() & Displaying


#include<stdio.h>

void main()
{
  char ch;
  printf(“Press any character: ”);
  ch = getchar();
  printf(“\nPressed character is: %c”, ch);
}

Output of the above program :

Run #1 :
-------------------------------
Press any character: e↲
Pressed character is: e

Run #2 :
-------------------------------
Press any character: efgd↲
Pressed character is: e

-------------------------------
Note: while using getchar() you 
can enter as many character but 
only first charcater is read.