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
Call
What 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
Parameter
Description
None
sync() takes no parameters.
Return Value
Return Value
Description
0
The synchronization worked.
-1
It 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;
}
Output
sync returned: 0
next char: a
How It Works
A string stream keeps all its text in memory, so there is nothing to bring in line with an outside source.
sync() succeeds and returns 0.
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;
}
Output
sync returned: -1
stream ok: no
How It Works
std::istream in(nullptr) makes an input stream that is not connected to any buffer.
sync() has nothing to synchronize, so it returns -1.
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;
}
Output
was synced with C I/O: yes
How It Works
By default C++ streams are synchronized with C input and output, so you can mix cout and printf safely.
sync_with_stdio(false) turns this off, which can make cin and cout faster. It returns the old setting.
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.