Table of contents
1.
Introduction
2.
Getchar in C++
3.
getchar() prototype
4.
getchar() Parameters
5.
getchar() Return value
6.
Uses
7.
Examples
8.
Frequently Asked Questions
8.1.
What is the getchar() function in C++?
8.2.
What is the prototype of getchar() function?
8.3.
What are the parameters of getchar() function?
8.4.
What is the return value of getchar() function?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Getchar() Function in C++

Author Nilesh Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninjas! This blog will dive into the getchar() function, a useful function for reading single characters from a user input or a file. The getchar() function is part of the C standard library and is defined in the <cstdio> header. It reads a single character from the standard input (usually the keyboard) and returns it as an int value. 

Getchar in cpp

This blog will explore its syntax, usage, and some examples to help you understand how to use it in your programs. So, let's get started!

Also see, Literals in C, Fibonacci Series in C++

Getchar in C++

The getchar() C++ function is a function from the C standard library used to read a single character from the standard input, typically the keyboard. It is defined in the <cstdio> header and is used in the following way:

int c = getchar();

 

The getchar() function reads a single character from the standard input and returns it as an int value. If the end of the input is reached, it returns EOF (end of file).

Here's an example of how you can use the getchar() function to read characters from the keyboard and print them out:

#include 
using namespace std;

int main() {
    char c;
    cout << "Enter a character: ";
    c = getchar();
    cout << "You entered: " << c << endl;
    return 0;
}

 

Output:

This C++ program, when run, will output the following:

Enter a character: 

 

This is the text prompt asking the user to enter a character. After the user inputs a character, for example, 'a', the program will output the following:

You entered: a

 

It will display the entered character as an output on the screen. The "return 0" in the last line signifies the end of the program, and 0 is returned to the operating system to indicate that the program terminated successfully.

In this example, the getchar C++ function reads a single character from the standard input (the keyboard) and stores it in the variable c. The character is then printed out on the screen.

It is important to note that the this function reads only one character at a time, so if you want to read a string or a line, you would need to use a loop to read characters one by one and then append them to a string.

In short, getchar C++ function is used to read a single character from the standard input and return it as an int value. It is defined in the header and helpful for reading user input, a file, or a stream.

getchar() prototype

The prototype for the getchar C++ function is as follows:

int getchar(void);

 

In this prototype, the getchar() function takes no arguments and returns an int value. The return value is the character that was read, or EOF (end of file), if the end of the input is reached.

Note:

getchar() function reads a single character from the standard input (usually the keyboard) and returns it as an int value. If you want to read a string or a line, you need to use a loop to read characters one by one and then append them to a string.

getchar() Parameters

The getchar C++ function takes no parameters. It reads a single character from the standard input (usually the keyboard) and returns it as an int value. This function has no parameters, meaning you do not need to pass any arguments when you call it in your program.

getchar() Return value

The getchar C++ function returns the character read from the standard input (usually the keyboard) as an int value.

It returns an int value which is the representation of the character that was read. It will return EOF (end of file) if the end of the input is reached. EOF is a macro that is defined in <cstdio>, and it is typically -1.

For example, if the user enters the letter 'a', this function will return the integer value 97, which is the ASCII value of the letter 'a'.

Uses

"getchar()" is a function in C++ that is used to read a single character from the standard input (usually the keyboard). It can be used to read input from the user in a variety of ways, such as:

  1. Reading a single character and storing it in a variable
     
  2. Reading multiple characters in a loop and processing them one at a time
     
  3. Reading a string of characters by calling "getchar()" in a loop until a specific delimiter is reached
     
  4. Reading a file one character at a time
     
  5. Reading password input by disabling the output of the entered characters on the screen
     
  6. Reading a stream of input with multiple lines and spaces.
     

It does not skip whitespaces, so it is useful for reading input with spaces or newlines. It is more efficient than using cin>> for reading a single character.

Examples

Here's an example of how you can use the getcharC++ function to read characters from the keyboard and print them out:

#include <iostream>
using namespace std;

int main() {
    char c;
    cout << "Enter a character: ";
    c = getchar();
    if(c != EOF)
        cout << "You entered: " << c << endl;
    else 
        cout << "End of File reached"<<endl;
    return 0;
}

 

In this example, the getchar C++ function reads a single character from the standard input (the keyboard) and stores it in the variable c. The program then checks if the value of c is not equal to EOF. If it's true, it will print the entered character. Otherwise, it will print "End of File reached."

Output:

The program will output the following:

Enter a character: a
You entered: a

 

This example shows how you can use the getchar() function to read a single character from the user's input and how you can check if the input is EOF and handle it properly.

Check out this article - C++ String Concatenation

Frequently Asked Questions

What is the getchar() function in C++?

The getchar C++ function is a function from the C standard library that reads a single character from the standard input, typically the keyboard. It is defined in the header and is used to read a single character and return it as an int value.

What is the prototype of getchar() function?

The prototype of getchar() function is int getchar(void).

What are the parameters of getchar() function?

 The getchar() function takes no parameters.

What is the return value of getchar() function?

The getchar() function returns the character read from the standard input as an int value. If the end of the input is reached, it returns EOF (end of file), typically -1.

Conclusion

Hey Ninja! We hope you enjoyed reading this article. We discussed the Getchar() Function in Cpp

Further, we discussed the getchar() prototype, getchar() parameter, and getchar() return value, and examples. You can also refer to blogs on 

Do visit our website to read more such blogs. Make sure you enroll in our courses. You can take mock testssolve problems, and interview puzzles. Also, you can check out some exciting interview stuff- interview experiences and an interview bundle for placement preparations. 

Happy Coding! 

Live masterclass