Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::ios_base::sync_with_stdio — synchronizing C++ and C streams

std::ios_base::sync_with_stdio controls synchronization between the standard C++ streams and C library streams. Calling it with false allows them to operate independently; the function returns the previous state, not a success indicator.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

static bool sync_with_stdio(bool sync = true);

std::ios_base::sync_with_stdio is a static function controlling synchronization between the standard C++ streams and their corresponding C streams, such as std::cout and stdout. Synchronization is initially enabled. The argument false allows the streams to operate independently; true, also the default when the argument is omitted, requests synchronization.

The bool result describes the previous state, not whether the operation succeeded: true means synchronization was previously enabled. The first call returns true. A call without an argument is therefore not a passive query of the setting.

Set the mode before any input or output operation on the standard streams. After such an operation, the effect of the call is implementation-defined, including an attempt to enable synchronization again. With synchronization enabled, operations on corresponding C++ and C streams can be interleaved; after disabling it, do not assume that this guarantee remains available.

Disabling synchronization does not guarantee a speedup: the standard specifies stream independence, not execution time. The example changes the setting before its first output, prints the previous state as 1, and then prints the computed sum. It does not mix C and C++ output operations.

Examples

#include <iostream>

int main() {
    const bool previous = std::ios_base::sync_with_stdio(false);
    std::cout << "previous=" << previous << '\n';
    std::cout << "sum=" << (20 + 22) << '\n';
}

Result

previous=1
sum=42

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources