The Wayback Machine - https://web.archive.org/web/20240118170042/https://www.geeksforgeeks.org/cpp-getchar-function/
Open In App
Related Articles

C++ getchar() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

getchar( ) is a function that takes a single input character from standard input. The major difference between getchar( ) and getc( ) is that getc( ) can take input from any number of input streams but getchar( ) can take input from a single standard input stream.

It is present inside the stdin.h C library. Just like getchar, there is also a function called putchar( ) that prints only one character to the standard output screen

Syntax:

int getchar(void);

Return Type: The input from the standard input is read as an unsigned char and then it is typecasted and returned as an integer value(int) or EOF(End Of File). A EOF is returned in two cases, 1. when file end is reached. 2. When there is an error during execution.

Example:

C++




// C++ Program to implement
// Use of getchar()
#include <cstdio>
#include <iostream>
  
using namespace std;
  
int main()
{
    char x;
  
    x = getchar();
    cout << "The entered character is : " << x;
  
    return 0;
}


Output: 

Output of the Program

Output

Example :

C++




// C++ Program to implement
// Use of getchar() and putchar()
#include <cstdio>
#include <iostream>
  
using namespace std;
  
int main()
{
    char x;
  
    x = getchar();
  
    cout << "The entered character is : ";
    putchar(x);
  
    return 0;
}


Output: 

Output of the Program

Output


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 27 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials