The Standard Output Stream

In C++, there are facilities for performing input and output known as “streams.” The example programs throughout this book use streams to read and display information. The name cout represents the standard output stream. You can use cout to display information.

cout << "Hello, world\n";

The string “Hello, world\n” is sent to the standard output device, which is the screen. The << operator is called the “insertion” operator. It points from what is being sent (the string) to where it is going (the screen).<$I<<(insertion operator);tutorial information>

Suppose you want to print an integer instead of a string. In C you would use printf with a format string that describes the parameters:

printf( "%d", amount );

In C++, you don't need the format string:

#include <iostream.h>

void main()

{

int amount = 123;

cout << amount;

}

The program prints 123.

You can send any built-in data types to the output stream without a format string. The cout stream is aware of the different data types and interprets them correctly.

The following example shows how to send a string, an integer, and a character constant to the output stream using one statement.

#include <iostream.h>

void main()

{

int amount = 123;

cout << "The value of amount is " << amount << '.';

}

This program sends three different data types to cout: a string literal, the integer amount variable, and a character constant '.' to add a period to the end of the sentence. The program prints this message:

The value of amount is 123.

Notice how multiple values are displayed using a single statement: the << operator is repeated for each value.