C – Unformatted Input Functions

Preview
You must first complete Read values in C- scanf() function before viewing this Lesson

Unformatted Input functions

Various unformatted input functions provided by C are:



1. getchar()

It is used to read value of a character variable. While using this function, we need to assign this function to the character variable whose value we want to read. The header file required for this function is <stdio.h>.

Program to demonstrate getchar()

Output

#include<stdio.h>
int main()
{
char n;
printf(“\nEnter character=”);
n=getchar();
printf(“\ncharacter=%c”,n);
return (0);
}
Enter character=K
character=K

2. getch()

It is used to read value of a character variable but while inputting value cursor immediately jumps to next line without need to press enter key . Moreover it doesn’t display anything on screen.

Program to demonstrate getch()Output
#include<stdio.h>
int main()
{
char n1;
printf(“\nEnter character=”);
n1=getch();
printf(“\nentered character=%c”,n1);
return (0);
}
Enter character=
character=K



3. getche()

It is used to read value of a character variable. Cursor automatically jumps to next line when we type any character but it displays the typed character on screen 

Program to demonstrate getche()Output
#include<stdio.h>
#include<conio.h>
int main()
{
char n;
printf(“\nEnter character=”);
n=getche();
printf(“\ncharacter=%c”,n);
getch();
return (0);
}
Enter any character=H
character=H

4.  gets()

It is used to read value of a string variable. It can also read blank space if any exists in the value.  Header file for this function is <stdio.h>.

Read string variable using gets()

Output

#include<stdio.h>
void main()
{
char bname[30];
printf(“\nEnter book name=”);
gets(bname);
printf(“\nBook Name=%s”,bname);
return (0);
}
Enter bookname=C Language
Book Name=C Language

Best Books of C




Lesson tags: getch function of c, getchar function of c, getche function of c, gets function of c
Back to: C Programming Language