C++ std::istream::get() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, get() reads exactly one character from an input stream. Unlike cin >> c, it does not skip spaces, tabs, or new lines. Every character is returned just as it was typed.

It has three common forms: get() returns the character, get(c) stores it in a char, and get(buffer, n) reads a short piece of text into a character array. Include <iostream> to use it.

This is a member function of std::istream. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Call get on an input stream. Choose the form that matches what you need.

C++
int ch = in.get();             // returns the character, or EOF
in.get(c);                     // stores it in c, returns the stream
in.get(buffer, 20);            // reads up to 19 chars and adds '\0'
CallWhat it does
in.get()Returns the next character as an int. At the end of input it returns EOF.
in.get(c)Stores the next character in c. The stream is returned, so it works in a while loop.
in.get(buf, n)Reads up to n - 1 characters, stops before the new line, and adds a at the end.

Use cin >> c when you want to skip spaces and get() when every character matters.

Library declarations
C++
int_type get();
istream& get(char_type& ch);
istream& get(char_type* s, streamsize count);
istream& get(char_type* s, streamsize count, char_type delim);

Parameters

ParameterDescription
chUsed by the second form. A char variable that receives the character.
sUsed by the buffer form. A character array where the text is stored.
countUsed by the buffer form. The size of the array. At most count - 1 characters are read, and then a \0 is added.
delimOptional, buffer form only. The character that stops the read. The default is '\n'. The delimiter is left in the stream.

Return Value

Return ValueDescription
int_typeThe no-argument form returns the character, or EOF when there is no more input.
istream&The other forms return the stream. It is false in a condition when nothing could be read.

C++ Examples

Example 1 — Read Every Character in a Loop

Print each character of a short text on its own line. The space is not skipped.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("Hi C");
    char c;

    while (in.get(c)) {
        std::cout << "[" << c << "]\n";
    }
    return 0;
}

How It Works

  1. in.get(c) reads one character into c and returns the stream, which is true while a character was read.
  2. The space between Hi and C is a real character. get() returns it, so it is printed inside the brackets.
  3. When the text ends, get(c) fails and the loop stops.

Example 2 — get() Returns an int

Read one character with no arguments and detect the end of input.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("A");

    int first = in.get();
    std::cout << "first: " << static_cast<char>(first) << '\n';

    int second = in.get();
    if (second == std::char_traits<char>::eof()) {
        std::cout << "no more input\n";
    }
    return 0;
}

How It Works

  1. in.get() returns an int. The first call returns the code of A, and static_cast<char> turns it back into a letter.
  2. The text has only one character, so the second call has nothing to give and returns the special end value.
  3. The result is an int on purpose: it must be able to hold every character and the end marker.

Example 3 — get(buffer, n) and the New Line

Read a line into a character array and see that the new line stays in the stream.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("one two\nthree");
    char buf[20];

    in.get(buf, 20);
    std::cout << "buffer: " << buf << '\n';

    char next = in.get();
    std::cout << "next is newline: " << (next == '\n' ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. get(buf, 20) copies characters into buf until it meets the new line. It adds a \0 so buf is a proper C string.
  2. The text one two is stored. The new line is the delimiter, and the stream stops just before it.
  3. The next get() returns that waiting '\n'. This is why getline() is easier for whole lines.

Frequently Asked Questions

It reads one character from the input, including spaces and new lines. Pressing Enter gives you the \n character.
cin >> c skips spaces and new lines before reading. cin.get(c) reads the very next character, whatever it is.
It must return every possible character plus a special EOF value to mark the end of the input. A plain char cannot hold both safely.
Both stop at the new line. get() leaves the new line in the stream, while getline() removes it. This makes getline() safer for reading line after line.
Calling cin.get() at the end of main() makes the program wait until you press Enter. Remember that leftover characters from earlier input may satisfy it at once.

Next: getline()

Read a whole line of text, spaces included.

getline() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful