So far, the examples haven't sent formatted output to cout. Suppose you want to display an integer using hexadecimal instead of decimal notation. The printf function handles this well. How does cout do it?
Note:
Whenever you wonder how to get C++ to do something that C does, remember that the entire C language is part of C++. In the absence of a better way, you can revert to C.
C++ associates a set of “manipulators” with the output stream. They change the default format for integer arguments. You insert the manipulators into the stream to make the change. The manipulators' names are dec, oct, and hex.
The next example shows how you can display an integer value in its three possible configurations.
#include <iostream.h>
main()
{
int amount = 123;
cout << dec << amount << ' '
<< oct << amount << ' '
<< hex << amount;
}
The example inserts each of the manipulators (dec, oct, and hex) to convert the value in amount into different representations.
This program prints this:
123 173 7b
Each of the values shown is a different representation of the decimal value 123 from the amount variable.