7. A Sum of Numbers

Standard Streams

The programs we have written so far print out their results on standard output. Standard output is one of the three standard streams that are available to programs on virtually all the modern operating systems.

Those three standard streams are:

  • standard output stream,
  • standard input stream,
  • standard error stream, which is an output stream just like standard output is.

Standard streams are standard means for a program to communicate with the outside world. By using the word cout, a program can write to standard output. Standard output can be imagined as some kind of a pipe going out of the program. The program does not actually know where this pipe leads to. The program just pushes its output into the pipe's inlet.

The operating system will, in general, connect the outlet of the standard output stream with the monitor, so that the program's results can be observed. This can be overridden so that the standard output goes to the printer, or to some other program or device.

Similarly, standard input pipe leads into the program and the operating system usually connects the inlet of standard input with the keyboard (if not instructed otherwise), so that the program can receive the data being typed in. This feature will be demonstrated in the next chapter.

Many of the programs in this book will use standard input and standard output, as they are the simplest means of doing input and output. To display the contents of those two streams, the operating system will open a console window, as you have already seen. There you will be able to see the results of the programs you run and to type in the input data for those programs.

We will rarely present the exact data that programs write to standard output, but we will provide something similar instead. In general, programs in this book will be accompanied by contents of the console window. The console window will display the standard input, standard output and standard error streams joined together, which usually turns out to be quite convenient.