Home > Articles > Programming > C/C++

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

3.4 Output

The iostream library defines output for every built-in type. Further, it is easy to define output of a user-defined type. By default, values output to cout are converted to a sequence of characters. For example,

void f()
{
  cout << 10;
}

will place the character 1 followed by the character 0 on the standard output stream. So will

void g()
{
  int i = 10;
  cout << i;
}

Output of different types can be combined in the obvious way:

void h(int i)
{
  cout << "the value of i is ";
  cout << i;
  cout << '\n';
}

If i has the value 10, the output will be

the value of i is 10

A character constant is a character enclosed in single quotes. Note that a character constant is output as a character rather than as a numerical value. For example,

void k()
{
  cout << 'a';
  cout << 'b';
  cout << 'c';
}

will output abc.

People soon tire of repeating the name of the output stream when outputting several related items. Fortunately, the result of an output expression can itself be used for further output. For example:

void h2(int i)
{
  cout << "the value of i is " << i << '\n';
}

This is equivalent to h().

  • + Share This
  • 🔖 Save To Your Account