Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

A Brief Look at cout

In Lesson 27, you will see how to use cout to print data to the screen. For now, you can use cout without fully understanding how it works. To print a value to the screen, write the word cout, followed by the insertion operator (<<), which you create by typing the less-than character (<) twice. Even though this is two characters, C++ treats it as one.

Follow the insertion character with your data. Listing 2.2 illustrates how this is used. Type in the example exactly as written, except substitute your own name where you see Jesse Liberty (unless your name is Jesse Liberty).

Listing 2.2. Using cout

  1:  // Listing 2.2 using std::cout
  2:  #include <iostream>
  3:  int main()
  4:  {
  5:      std::cout << "Hello there.\n";
  6:      std::cout << "Here is 5: " << 5 << "\n";
  7:      std::cout << "The manipulator std::endl ";
  8:      std::cout << "writes a new line to the screen.";
  9:      std::cout <<  std::endl;
 10:      std::cout << "Here is a very big number:\t" << 70000;
 11:      std::cout << std::endl;
 12:      std::cout << "Here is the sum of 8 and 5:\t";
 13:      std::cout << 8+5 << std::endl;
 14:      std::cout << "Here's a fraction:\t\t";
 15:      std::cout << (float) 5/8 << std::endl;
 16:      std::cout << "And a very very big number:\t";
 17:      std::cout << (double) 7000 * 7000 << std::endl;
 18:      std::cout << "Don't forget to replace Jesse Liberty ";
 19:      std::cout << "with your name...\n";
 20:      std::cout << "Jesse Liberty is a C++ programmer!\n";
 21:      return 0;
 22:  }


Hello there.
Here is 5: 5
The manipulator endl writes a new line to the screen.
Here is a very big number:      70000
Here is the sum of 8 and 5:     13
Here's a fraction:              0.625
And a very very big number:     4.9e+007
Don't forget to replace Jesse Liberty with your name...
Jesse Liberty is a C++ programmer!


The statement #include <iostream> causes the iostream file to be added to your source code. This is required if you use cout and its related functions.

The program starts with the the simplest use of cout by printing a string; that is, a series of characters. The symbol \n is a special formatting character. It tells cout to print a newline character to the screen; it is pronounced “slash-n” or “new line.”

Three values are passed to cout in this line:

    std::cout << "Here is 5: " << 5 << "\n";

In here, each value is separated by the insertion operator (<<). The first value is the string "Here is 5: ". Note the space after the colon. The space is part of the string. Next, the value 5 is passed to the insertion operator and then the newline character (always in double quotes or single quotes) is passed. This causes the line

Here is 5: 5

to be printed to the console. Because no newline character is present after the first string, the next value is printed immediately afterward. This is called concatenating the two values.

Note the usage of the manipulator std::endl. The purpose of endl is to write a new line to the console, thus presenting an alternative to ‘\n’. Note that endl is also provided by the standard library; thus, std:: is added in front of it just as std:: was added for cout.

The formatting character \t inserts a tab character. Other lines in the sample demonstrate how cout can display integers, decimal equivalents, and so on. The terms (float) and (double) tell cout that the number is to be displayed as a floating-point value. All this will be explained in Lesson 3, “Using Variables, Declaring Constants,” when data types are discussed.

You should have substituted your name for Jesse Liberty. If you do this, the output should confirm that you are indeed a C++ programmer. It must be true, because the computer said so!

  • + Share This
  • 🔖 Save To Your Account