Displaying Output on Standard Output or Standard Error

The I/O Stream Class Library predefines three output streams, as well as the cin input stream. The standard output stream is cout, and the remaining streams, cerr and clog, are standard error streams. Output to cout goes to the C standard output stream, stdout, unless cout has been redirected. Output to cerr and clog goes to the C standard error stream, stderr, unless cerr or clog has been redirected.

cerr and clog are really two streams that write to the same output device. The difference between them is that cerr flushes its contents to the output device after each output, while clog must be explicitly flushed.

You can print to one of the predefined output streams by using the predefined stream's name and the output operator (operator<<), followed by the value to print:

#include <iostream.h>
void main(int argc, char* argv[]) {
   if (argc==1) cout << "Good day!" << endl;
   else cerr << "I don't know what to do with "
             << argv[1] << endl;
}

If you name the compiled program myprog, the following inputs will produce the following output to standard output or standard error:

Invocation Output
myprog
Good day!
(to standard output)
myprog hello there
I don't know what to do with hello
(to standard error)

An output operator must exist for any type being output. The I/O Stream Class Library defines output operators for all C++ built-in types. For types you define yourself, you need to provide your own output operators. If you attempt to place the contents of a variable into an output stream and no output operator is defined for the type of that variable, the compiler displays an error message with text similar to the following:

The call does not match any parameter list for "operator<<".

Multiple Variables in an Output Statement

You can place a succession of variables into an output stream with a single output statement, by repeating the output operator (<<) after each output, and then specifying the next variable to output. You can combine variables of multiple types in an output statement, without having to specify the types of those variables in the output statement. For example:

int i,j,k;
float l,m;
// ...
cout << i << j << k << l << m;

The above syntax provides identical results to the following multiple output statements:

int i,j,k;
float l,m;
cout << i;
cout << j; 
cout << k;
cout << l;
cout << m; 

If you want to enhance the readability of your source code, break the single output statement up with white space, instead of separating it into multiple output statements:

int i,j,k;
float l,m;
cout << i
     << j
     << k
     << l
     << m; 

Using Output Streams Other Than cout, cerr, and clog

You can use the same techniques for output to other output streams as for output to the predefined output streams. The only difference is that, for other output streams, your program must define the stream. Assuming you have defined a stream attached to a file opened for output, and have called that stream myout, you can write to that file through its stream, by specifying the stream's name instead of cout, cerr or clog:

// assume the output file is associated with stream myout
   int a,b;
   myout << a << b;


Opening a File for Output and Writing to the File