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

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, unget() makes an input stream take one step back. The character you read last becomes the next character again, so reading from the stream returns it a second time.

It is the simpler cousin of putback(). You do not name the character. The stream already knows which one to give back. 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 unget on an input stream right after reading a character. It takes no arguments.

C++
char c;
in.get(c);
in.unget();           // c will be read again
CallWhat it does
in.unget()Moves back one character.
in.get(c); in.unget();Read a character and take it back at once.
if (!in.unget())Tests whether stepping back worked.

unget() is good for one step back. Do not rely on going back several characters in a row.

Library declarations
C++
istream& unget();

Parameters

ParameterDescription
Noneunget() takes no parameters.

Return Value

Return ValueDescription
istream&A reference to the stream. If stepping back is not possible, badbit is set and the stream is false in a condition.

C++ Examples

Example 1 — Read a Character and Take It Back

The same letter is read twice because unget() undoes the first read.

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

int main() {
    std::istringstream in("Hello");
    char c = 0;

    in.get(c);
    std::cout << "read: " << c << '\n';

    in.unget();

    in.get(c);
    std::cout << "read again: " << c << '\n';
    return 0;
}

How It Works

  1. The first get(c) reads H. The read position moves one step forward.
  2. in.unget() moves the read position one step back, so H is the next character again.
  3. The second get(c) reads H again.

Example 2 — Skip Letters, Then Read the Number

Read letters until a digit appears, step back, and read the whole number with >>.

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

int main() {
    std::istringstream in("ab12");
    std::string letters;
    char c = 0;

    while (in.get(c) && std::isalpha(c)) {
        letters += c;
    }
    in.unget();

    int n = 0;
    in >> n;

    std::cout << "letters: " << letters << '\n';
    std::cout << "number: " << n << '\n';
    return 0;
}

How It Works

  1. The loop collects a and b. It stops when get(c) reads the 1, because 1 is not a letter.
  2. The 1 has already been read, so unget() gives it back.
  3. Now in >> n sees 12 from its first digit and reads the whole number.

Example 3 — unget() After the End of the Text

Read the only character, hit the end, and go back one step to read it again.

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

int main() {
    std::istringstream in("Z");
    char c = 0;

    in.get(c);
    in.get(c);
    std::cout << "second read ok: " << (in ? "yes" : "no") << '\n';

    in.clear();
    in.unget();
    in.get(c);
    std::cout << "after unget: " << c << '\n';
    return 0;
}

How It Works

  1. The first get() reads Z. The second one finds nothing and sets the stream flags, so the stream is not ok.
  2. clear() resets those flags. The read position is still after Z.
  3. unget() steps back one character, so the next get() returns Z again.

Frequently Asked Questions

It moves the read position of an input stream back by one character, so the last character read will be read again.
putback(c) takes the character as an argument. unget() takes no argument and gives back whichever character was read last.
Usually for one character, but it is not guaranteed. If it fails, the stream sets badbit, so check the result when it matters.
You should not count on it. Streams promise only one character of step back. Use a std::string for more look-ahead.
peek() looks at the next character without reading it, which is cleaner. Use unget() when you have already read the character and then decide you did not want it.

Next: unsetf()

Turn off one or more stream format flags.

unsetf() 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.

15 people found this page helpful