Using C and C++ standard streams and redirection

The standard streams are declared in the C header file stdio.h or in the C++ header files iostream.h or iostream. Table 1 shows the C standard streams and the functions that use them. Table 2 shows the C++ standard streams and the operators typically used to perform I/O with them.

By default, the standard streams are opened implicitly the first time they are referenced. You do not have to declare them or call their open() member functions to open them. For example, with no preceding declaration or open() call, the following statement writes the decimal number n to the cout stream.
cout << n << endl;
For more detailed information about C++ I/O streaming, see the following:
Table 1. C standard streams
Stream name Purpose Functions that use it
stdin The input device from which your C program usually retrieves its data.

getchar()
getchar_unlocked()
gets()
gets_unlocked()
scanf()
scanf_unlocked()
vscanf()
vscanf_unlocked()
wscanf()
wscanf_unlocked()
vwscanf()
vwscanf_unlocked()

stdout The output device to which your C program normally directs its output.

printf()
printf_unlocked()
puts()
puts_unlocked()
putchar()
putchar_unlocked()
vprintf()
vprintf_unlocked()
wprintf()
wprintf_unlocked()
vwprintf()
vwprintf_unlocked()

stderr The output device to which your C program directs its diagnostic messages. z/OS® XL C/C++ uses stderr to collect error messages about exceptions that occur.

perror()
perror_unlocked()

Table 2. C++ standard streams
Stream name Purpose Common usage
cin The object from which your C++ program usually retrieves its data. In z/OS XL C++, input from cin comes from stdin by default. >>, the input (extraction) operator
cout The object to which your C++ program normally directs its output. In z/OS XL C++, output to cout goes to stdout by default. <<, the output (insertion) operator
cerr The object to which your C++ program normally directs its diagnostic messages. In z/OS XL C++, output to cerr goes to stderr by default. cerr is unbuffered, so each character is flushed as you write it. <<, the output (insertion) operator
clog Another object intended for error messages. In z/OS XL C++, output to clog goes to stderr by default. Unlike cerr, clog is buffered. <<, the output (insertion) operator

On I/O operations requiring a file pointer, you can use stdin, stdout, or stderr in the same manner as you would any other file pointer.

If you are running with POSIX(ON), standard streams are opened during initialization of the process, before the application receives control. With POSIX(OFF), the default behavior is for the C standard streams to open automatically on first reference. You do not have to call fopen() to open them. For example, if the following statement is specified without a preceding fopen() statement, it writes the decimal number n to the stdout stream.
   printf("%d\n",n);

By default, stdin interprets the character sequence /* as indicating that the end of the file has been reached. See Performing terminal I/O operations for more information.