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

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, peek() lets you look at the next character of an input stream without removing it. The character stays in the stream, so the next get() or >> still reads it.

This is useful for look-ahead: you decide what to do based on the next character before you read it. 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 peek on an input stream. It takes no arguments.

C++
int next = in.peek();

if (std::isdigit(in.peek())) {
    // a digit comes next
}
CallWhat it does
in.peek()Returns the next character as an int and leaves it in the stream.
in.peek() == '\n'Checks whether the next character is a new line.
in.peek() == std::char_traits<char>::eof()Checks whether there is nothing more to read.

Compare the result with a char like 'a' directly. Store it in an int, not a char, if you also want to detect the end.

Library declarations
C++
int_type peek();

Parameters

ParameterDescription
Nonepeek() takes no parameters.

Return Value

Return ValueDescription
int_typeThe next character, or EOF (usually -1) when the input has ended.

C++ Examples

Example 1 — peek() Does Not Remove the Character

Peek twice, then get once, and see the difference.

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

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

    std::cout << "peek: " << static_cast<char>(in.peek()) << '\n';
    std::cout << "peek: " << static_cast<char>(in.peek()) << '\n';
    std::cout << "get:  " << static_cast<char>(in.get()) << '\n';
    std::cout << "peek: " << static_cast<char>(in.peek()) << '\n';
    return 0;
}

How It Works

  1. The two peek() calls both show X, because nothing was taken out of the stream.
  2. get() reads X for real and removes it.
  3. Now the first character in the stream is Y, so the last peek() shows Y.

Example 2 — Read Digits While the Next One Is a Digit

Use peek() to stop at the first character that is not a digit, without eating it.

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

int main() {
    std::istringstream in("123abc");
    int number = 0;

    while (std::isdigit(in.peek())) {
        number = number * 10 + (in.get() - '0');
    }

    std::cout << "number: " << number << '\n';
    std::cout << "next: " << static_cast<char>(in.peek()) << '\n';
    return 0;
}

How It Works

  1. std::isdigit(in.peek()) checks the next character. The digit is not removed yet.
  2. If it is a digit, in.get() takes it and - '0' turns the character into its value. The number grows to 1, 12, then 123.
  3. When the next character is a, the loop stops. Because peek() never removed it, the letter is still in the stream.

Example 3 — Check for an Empty Stream

Peek at the first character to find out whether anything is there.

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

void check(const std::string& text) {
    std::istringstream in(text);
    if (in.peek() == std::char_traits<char>::eof()) {
        std::cout << "empty\n";
    } else {
        std::cout << "has data\n";
    }
}

int main() {
    check("A");
    check("");
    return 0;
}

How It Works

  1. check() builds a string stream from the text it gets.
  2. For "A", peek() returns the code of A, which is not the end marker, so the stream has data.
  3. For the empty text, there is nothing to look at, so peek() returns the end marker and the function prints empty.

Frequently Asked Questions

It returns the next character of the input without removing it. The next read still gets that same character.
It returns EOF, which is usually -1, and sets eofbit. Compare with std::char_traits<char>::eof() to be safe.
get() removes the character it returns. peek() only looks at it. Calling peek() many times always gives the same result.
It must be able to return every character value and also the special EOF value, so a plain char is not enough.
No. It shows only the next character. To look further, read characters and put them back with putback() or unget(), or use a std::string.

Next: put()

Write one character to an output stream.

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

11 people found this page helpful