Skip to the content of the web site.

Lesson 1.6: The binary << operator

Previous lesson Next lesson


Up to this point, we have seen what appears to be a binary << operator,

	std::cout << "Hello world!";
	std::cout << std::endl;

and we have described binary arithmetic operators. Later, we will see that the << has a specific meaning in C++ (it is called the left-shift operator), but that operators can be overloaded (that is, to mean something else) so that if the first argument is std::cout (representing console output) and the second argument is a string, the appropriate function is called to print the string to the console. Similarly, if the first argument is std::cout and the second argument is std::endl, the appropriate function is called to print a new line on to the console (that is, move the cursor to the start of the next line).

The overloading is actually somewhat appropriate: it looks like the second argument is being moved to console output:

	std::cout << 42;
	std::cout << std::endl;

Later, we will see that how C++ stores integers and how it stores real numbers are very different, so while you may think both of these are calling the same printing function, the second is printing a representation of a real number, whereas the first is printing a representation of an integer:

// Pre-processor include directives
#include <iostream>

// Function declarations
int main();

// Function definitions
int main() {
	std::cout << 1234;
	std::cout << std::endl;
	std::cout << 1234.0;
	std::cout << std::endl;
	std::cout << 123456789;
	std::cout << std::endl;
	std::cout << 123456789.0;
	std::cout << std::endl;

	return 0;
}

You can run a similar program at repl.it that attempts to display the limits of the various displays.

The output 1.234567e+06 should be interpreted as $1.23457 \times 10^6$.

A review of sequences of arithmetic operations

When you print

	std::cout << (1 + 2 + 3 + 4 + 5);

a sequence of operations must be performed. The processor will not add a list of integers. Instead, it will first calculate $1 + 2$ which returns $3$, then given that result, $3 + 3$ which returns $6$, then $6 + 4$ returns $10$ and $10 + 5$ returns $15$. Once we get the result of calculation, the program executes

	std::cout << 15;

This then prints the value $15$ to the console output.

Question: What does std::cout << 15 return?

A sequence of objects to be printed

When the computer executes the operation

	std::cout << 1;

the printing of $1$ is a side effect, but the actual returned value is a reference to the std::cout object.

Next, consider the following:

	(std::cout << 1) << 2;

Once the statement std::cout << 1 finishes, it returns a reference to the std::cout object, so what is left is std::cout << 2, which prints a $2$ and then returns. In this case, however, you don't need parentheses: as the default behavior is the execute the first << first:

	std::cout << 1 << 2;

For example, we could now rewrite the Hello world! program as

	std::cout << "Hello world!" << std::endl;

Notice, however, that it is your responsibility to ensure the spacing is correct. When we printed $1$ and $2$, the output was juxtaposed. If you wanted a space between them, you had to explicitly included it:

	std::cout << 1 << ' ' << 2;

You can view some of these examples and others at repl.it.

Guidelines for using std::cout <<

Not only must the output be legible when it is printed to the console, it is also useful that the source code is equally legible. Thus, while it is entirely possible to string together an arbitrary number of items to be printed to the screen, the first recommendation is to use one statement per line:

	std::cout << "Once a jolly swagman camped by a billabong"
	<< std::endl << "Under the shade of a coolibah tree," <<
	std::cout << "And he sang as he watched and waited till his \"Billy\" boiled,"
	<< std::endl << "\"You'll come a-waltzing Matilda, with me.\"" <<
	std::endl;

is less preferable than

	std::cout << "Once a jolly swagman camped by a billabong" << std::endl;
	std::cout << "Under the shade of a coolibah tree," << std::endl;
	std::cout << "And he sang as he watched and waited till his \"Billy\" boiled,"
	          << std::endl;
	std::cout << "\"You'll come a-waltzing Matilda, with me.\"" << std::endl;

You will also note that there is always at least one space around the operator, as it allows the eye to quickly pick out what is being printed. The above example is much clearer than, for example

	std::cout<<"Once a jolly swagman camped by a billabong"<<std::endl;
	std::cout<<"Under the shade of a coolibah tree,"<<std::endl;
	std::cout<<"And he sang as he watched and waited till his \"Billy\" boiled,"
	         <<std::endl;
	std::cout<<"\"You'll come a-waltzing Matilda, with me.\""<<std::endl;

Additionally, lining up the << operators is cleaner, too. Both of these produce the same output:

	std::cout << "The value " << 3.23 << " is greater than "
	<< 1.53 << "." << std::endl;

	std::cout << "The value "        << 3.23 
	          << " is greater than " << 1.53
	          << "." << std::endl;

Previous lesson Next lesson