Open In App

basic_istream::unget() in C++ with Examples

Last Updated : 15 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

The basic_istream::unget() is used to unget the character and used to decrease the location by one character and makes the extracted character available for used once again. 
Header File: 
 

<iostream>


Syntax: 
 

basic_istream& unget();


Parameters: The basic_istream::unget() method doesn’t accepts any parameter.
Return Value: The function basic_istream::unget() returns the basic_istream object.
Below is the programs to illustrate std::basic_istream::unget():
Program 1: 
 

CPP14
// C++ code for basic_istream::unget()
#include <bits/stdc++.h>
using namespace std;

// Driver code
int main()
{
    // Declare string stream
    istringstream gfg("GeeksforGeeks");

    char a = gfg.get();
    if (gfg.unget()) {
        char b = gfg.get();
        cout << "We got: " << a << endl;
        cout << "After ungetting the "
             << "character once again"
             << " we got: "
             << b << endl;
    }

    return 0;
}
Output:
We got: G
After ungetting the character once again we got: G


Program 2: 
 

CPP14
// C++ code for basic_istream::unget()
#include <bits/stdc++.h>
using namespace std;

// Driver code
int main()
{
    // Declare string stream
    istringstream gfg("Laptop");

    char a = gfg.get();
    if (gfg.unget()) {
        char b = gfg.get();
        cout << "We got: " << a << endl;
        cout << "After ungetting the "
             << "character once again"
             << " we got: "
             << b << endl;
    }

    return 0;
}
Output:
We got: L
After ungetting the character once again we got: L


Reference: https://cplusplus.com/reference/istream/basic_istream/unget/


Article Tags :
Practice Tags :

Similar Reads