C – Unformatted Input Function

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

C – Unformatted Input function

There are various input functions provided by C Language. These functions are used to read the value of a variable during the execution of program.

Various unformatted input functions provided by C Language are as follows:

1. getchar()

It stands for get character. It is used to read value of a character variable. When we use this function to read value of a character variable, it will not proceed to next line until we press enter key from the keyboard.

When we use 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:

charvariable=getchar();

charvariable refers to the character variable whose value we want to read using getchar() function.

Program to read character type variable using getchar()

Output

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

2. getch()

It stands for get character. It is used to read value of a character variable.
When we use this function to read value of a character variable, cursor automatically jumps to next line when we press any key from the keyboard.

We don’t need to press enter key from the keyboard after typing the value.

The main feature of this function is that it doesn’t display any character on screen when we type the value to be stored in 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 <conio.h>.

Syntax:

charvariable=getch();
charvariable refers to the character variable whose value we want to read using getch() function.

Program to read character type variable using getch()

Output

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

3. getche()

This function is used to read value of a character variable.

When we use this function to read value of a character variable, cursor automatically jumps to next line when we press any key from the keyboard. We don’t need to press enter key from the keyboard after typing the value. 

This function displays the character value on screen while typing the value to be stored in 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 <conio.h>.

Syntax:

charvariable=getche();

charvariable refers to the character variable whose value we want to read using getch() function.

Program to read character type variable using getche()

Output

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

4.  gets()

It stands for get string. It is used to read value of a string variable. String variable is basically a character array.

When we use gets() function to read value of a string variable, it will also read blank space if any blank space exists in the typed value. which is not possible using scanf() statement.

The string variable whose value we want to read must be specified as the argument to the gets() function. The header file required for this function is <stdio.h>.

Syntax:

gets(str);

str referes to the string variable whose value we want to read using gets() function. 

Program to read string variable using gets()

Output

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
printf(“\nEnter your name=”);
gets(name);
printf(“\nName=%s”,name);
getch();
return (0);
}
Enter your name=lovejot  bhardwaj
Name=lovejot  bhardwaj

Tips and tricks

  • The difference between getch() and getchar() is that we need to press enter key after typing a value in getchar() whereas cursor automatically jumps to next line in case of getch(). Moreover getch() doesn’t display the input.
  • The difference between getch() and getche() is that getche() displays the input whereas getch() doesn’t. We need not to press enter key after typing a value in both the cases.
  • gets() can read spaces in the intput whereas scanf() can’t do that.

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