C – Unformatted Input Functions

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

Unformatted Input functions in C Language

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>.

Syntax:

var=getchar();

var refers to the char type variable whose value we want to read.

Program to demonstrate getchar()

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

2. getch()

It is used to read value of a char type variable. Cursor immediately jumps to next line without need to press enter key when we input a value. Moreover it doesn’t display anything on screen.

Syntax:

var=getch();

var refers to the char type variable whose value we want to read.

Program to demonstrate getch()
#include<stdio.h>
int main()
{
char n1;
printf("\nEnter character=");
n1=getch();
printf("\nentered character=%c",n1);
return (0);
}
Output
Enter character=<'K' typed from keyboard but not visible>
character=K

3. getche()

It is used to read value of a char type variable. Cursor automatically jumps to next line when we type any character .

Syntax:

var=getche();

var refers to the char type variable whose value we want to read.

Program to demonstrate getche()
#include<stdio.h>
#include<conio.h>
int main()
{
 char n;
 printf("\nEnter character=");
 n=getche();
 printf("\ncharacter=%c",n);
 return (0);
}
Output
Enter 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>.

Syntax:

var=gets();

var refers to the string variable whose value we want to read.

Read string variable using gets()

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

 

Spread the love
Lesson tags: getch function of c, getchar function of c, getche function of c, gets function of c, Unformatted Input functions in C Language
Back to: C Programming Language
Spread the love