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

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, sync() asks an input stream to bring its internal buffer in line with the real source of the data. What this means in detail depends on the stream and on the compiler.

For a string stream it changes nothing and succeeds. For std::cin, some compilers discard unread input and others do nothing. 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 sync on an input stream. It takes no arguments and returns 0 or -1.

C++
int result = in.sync();     // 0 means success

std::ios::sync_with_stdio(false);   // a different function
CallWhat it does
in.sync()Synchronizes the buffer. Returns 0 on success.
std::cin.sync()On some systems throws away unread characters. Not portable.
std::ios::sync_with_stdio(false)Not the same thing. It stops cin and cout from staying in step with C input and output.

Do not use cin.sync() to empty the input buffer. Use cin.ignore() instead, because that works the same everywhere.

Library declarations
C++
int sync();

Parameters

ParameterDescription
Nonesync() takes no parameters.

Return Value

Return ValueDescription
0The synchronization worked.
-1It failed, or the stream has no buffer. With a failed buffer, badbit is set.

C++ Examples

Example 1 — sync() on a String Stream

For a string stream, sync() succeeds and leaves the data alone.

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

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

    int result = in.sync();
    char c = in.get();

    std::cout << "sync returned: " << result << '\n';
    std::cout << "next char: " << c << '\n';
    return 0;
}

How It Works

  1. A string stream keeps all its text in memory, so there is nothing to bring in line with an outside source.
  2. sync() succeeds and returns 0.
  3. The data is untouched, so the next get() still returns a.

Example 2 — sync() Without a Buffer

A stream with no buffer cannot synchronize, so the result is -1.

C++
#include <iostream>

int main() {
    std::istream in(nullptr);

    int result = in.sync();

    std::cout << "sync returned: " << result << '\n';
    std::cout << "stream ok: " << (in ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. std::istream in(nullptr) makes an input stream that is not connected to any buffer.
  2. sync() has nothing to synchronize, so it returns -1.
  3. A stream without a buffer is also in the bad state, so the stream test says no.

Example 3 — Do Not Confuse It with sync_with_stdio()

The static function std::ios::sync_with_stdio() returns the previous setting.

C++
#include <iostream>

int main() {
    bool was = std::ios::sync_with_stdio(false);

    std::cout << "was synced with C I/O: " << (was ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. By default C++ streams are synchronized with C input and output, so you can mix cout and printf safely.
  2. sync_with_stdio(false) turns this off, which can make cin and cout faster. It returns the old setting.
  3. The old setting was true, so the program prints yes. After this call, do not mix printf with cout.

Frequently Asked Questions

It asks cin to synchronize its buffer with the real input. The effect depends on the compiler: some discard unread characters, others do nothing.
It is not reliable. Use cin.ignore(1000, '\n') for a result that is the same everywhere.
0 when it worked and -1 when it failed or when the stream has no buffer.
sync() is a member of an input stream that synchronizes one buffer. sync_with_stdio() is a static function that links or unlinks C++ streams and the C library for the whole program.
It makes cin and cout faster by removing the link to C input and output. After that, do not use printf and scanf in the same program.

Next: tellg()

Get the current read position of an input stream.

tellg() 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