Interleaving the standard streams without sync_with_stdio()

Output can be interleaved without sync_with_stdio(), since the C++ standard streams are based on z/OS® XL C I/O. That is, cout can be interleaved with stdout, and clog can be interleaved with stderr. This is done by explicitly flushing cout or clog before calling the z/OS XL C output function. Results of attempting to interleave these streams without explicitly flushing, are undefined. Output to cerr doesn't have to be explicitly flushed, since cerr is unit-buffered.

Input to cin may be interleaved with input to stdin, without sync_with_stdio(), on a line-by-line basis. Results of attempting to interleave on a per-character basis are undefined. Figure 1 shows an example program and the output it produces.

Figure 1. Interleaving I/O without sync_with_stdio()
// Example of interleaving I/O without sync_with_stdio()
//
// tsyncwos.cxx
#include <stdio.h>
#include <fstream.h>

int main() {
    cout << "object: to illustrate interleaving input and output\n    "
            "    without sync_with_stdio()\n" << endl;

    printf( "interleaving output ");
    cout << "works with an (end of line 1)   \n" << flush;
    cout << "explicit flush of cout            " << flush;
    printf( "(end of line 2)\n\n");

    char  string1[80] = "";
    char  string2[80] = "";
    char  string3[80] = "";
    char* rc = NULL;

    cout << "type the following 3 lines:\n"
            "interleaving input\n"
            "on a per-line basis\n"
            "is supported\n" << endl;

    cin.getline(string1, 80);
    rc = gets(string2);
    cin.getline(string3, 80);

    cout << "\nstring1 is \"" << string1 << "\"\n"
         <<   "string2 is \"" << string2 << "\"\n"
         <<   "string3 is \"" << string3 << "\"\n" << endl;
                         // The endl manipulator inserts a newline
                         // character and calls flush().

    char  char1 = '\0';
    char  char2 = '\0';
    char  char3 = '\0';

    cout << "type the following 2 lines:\n"
            "results of interleaving input on a per-\n"
            "character basis are not defined\n" << endl;

    cin  >> char1;
    char2 = (char) getchar();
    cin  >> char3;

    cout << "\nchar1   is \'" << char1 << "\'\n"
         <<   "char2   is \'" << char2 << "\'\n"
         <<   "char3   is \'" << char3 << "\'\n" << flush;
}
// sample output (with user input shown underlined):
//
// object: to illustrate interleaving input and output
//         without sync_with_stdio()
//
// interleaving output works with an (end of line 1)
// explicit flush of cout            (end of line 2)
//
// type the following 3 lines:
// interleaving input
// on a per-line basis
// is supported
//
// interleaving-input
// on a per-line basis
// is supported
//
// string1 is "interleaving input"
// string2 is "on a per-line basis"
// string3 is "is supported"
//
// type the following 2 lines:
// results of interleaving input on a per-
// character basis are not defined
//
// results of interleaving input on a per-
// character basis are not defined
//
// char1   is 'r'
// char2   is 'c'
// char3   is 'e'