Introduction :
Handling user input is a fundamental task in any programming language. Generally, the scanf() function is used to take input from the user. You can also use getchar() function for this purpose. Using the getchar() function, you to read a single character from the keyboard (standard input).
In this article, I shall show you how to use getchar() function using the C programming language. Here, I shall also show you the syntax and usage of the getchar() function.
What is getchar() function :
The getchar() is a standard input output library (stdio.h) function which used to read a single character from the keyboard. It is defined in the stdio.h library or header file. It is useful where you need one character at a time. The getchar() reads all characters including whitespace characters (spaces, tabs and new lines) and special characters.
Syntax of the getchar() function :
In C programming language the following code is the syntax of the getchar() function.
int getchar(void);
The getchar() function does not take any parameter or argument and it returns an integer value (ASCII value) of the input character. It reads a single character but returns an int value. When the end of the input is reached or an error occurs, it returns EOF. The value of EOF defined as (-1).
Different types of programs using getchar() function :
In this article, I show you the handling of single and multiple characters input by getchar() function. I also show you the handle of the End of File in getchar() function.
Handling single character using getchar() function :
Using getchar() function, you can read a single character. After reading the character, it prints on the console screen using printf() function.
Source code of the program :
The following code is used to read a single character using getchar() function in C programming language.
/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
char char_input;
printf("Enter a single character \n");
char_input = getchar();
printf("You have entered %c\n", char_input);
return 0;
}
Output of the program :
You can see the output of handling single character using getchar() function like below image.

Handling multiple characters using getchar() function :
You can use the getchar() function in a loop to read multiple characters. The below program is the example of reading multiple characters using getchar() function. Here, you have to press “Enter” key to stop the program. The putchar() is used to display the input characters.
Source code of the program :
The following code is used to read multiple characters using getchar() function in C programming language.
/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
int char_input;
printf("Input a text and to finish press Enter button\n");
while ((char_input = getchar()) != '\n')
{
putchar(char_input);
}
printf("\n");
return 0;
}
Output of the program :
You can see the output of handling multiple characters using getchar() function like below image.

Handling End of File (EOF) using getchar() function :
Using getchar() function, you can handle end of file (EOF) conditions. When you enter “Ctrl+Z” and “Enter” button in Windows, the program will be stoped.
Source code of the program :
The following code is used to handle End of File using getchar() function in C programming language.
/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
int char_input;
printf("Input text and to finish enter (Ctrl+Z+Enter) button (in Windows)\n");
while ((char_input = getchar()) != EOF)
{
putchar(char_input);
}
printf("\nYour input is completed\n");
return 0;
}
Output of the program :
You can see the output of handling end of file using getchar() function like below image.

How run the programs :
If you want to run all the programs on your PC, you have to install VS Code. You can use any other C compiler. Then, open VS Code and create new file give name as “pac.c”. After that, copy the above code of program one by one and paste in the “pac.c” file and run all the programs on your PC.
Conclusion :
At last, you have learned how to use getchar() function in the C programming language. Now you can use this function to create your own programs. Thank you for visiting my site.