Stream Output
The ostream class provides both formatted and unformatted output:
- Output of standard data types with the stream-insertion operator
- Output of characters with the put member function
- Unformatted output with the write member function
- Output of integers in decimal, octal, and hexadecimal formats
- Output of floating point values with various precisions, with forced decimal points, in scientific notation and in fixed notation
- Output of data justified in fields of specified field widths
- Output of data in fields padded with specified characters
- Specification of uppercase letters in scientific notation and in hexadecimal notation.
Stream-Insertion Operator
Stream output may be performed using the stream-insertion operator:
The overloaded << operator
The operator is overloaded to provide type-safe output of built-in types (int, char, long, short, unsigned, double, float, etc., plus string types and pointer values)
For example:
//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
std::cout << "This is C++ output!\n";
std::cout << "This is more C++ ";
std::cout << "output...\n";
return 0;
}which outputs:
This is C++ output!
This is more C++ output...
Program ended with exit code: 0
endl and flush Stream Manipulators
You can also use the endl and flush stream manipulators:
//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
std::cout << "This is C++ output!";
std::cout << std::endl; // outputs newline and flushes
std::cout << "This is more C++ output!";
std::cout << std::flush;
return 0;
}which outputs:
This is C++ output!
This is more C++ output!Program ended with exit code: 0
flushis used to force any data still in the buffer to be output.
You can use whole expressions:
//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
int a = 5;
std::cout << "a + 5 = " << a + 5 << std::endl;
return 0;
}which outputs:
a + 5 = 10
Program ended with exit code: 0
But you have to be careful with precedence:
//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
int a = 5;
int b = 7;
int c = 9;
// But be careful about precedence:
std::cout << "a^b|c = " << (a^b|c) << std::endl; // Compile-time error
std::cout << "a^b|c = " << ( (a^b) |c) << std::endl; // OK
return 0;
}which produces a compile-time warning:
main.cpp:17:32 '^' within '|'
Place parentheses around the '^' expression to silence this warning
… and associativity:
//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
int a = 5;
int b = 7;
// Problem with associativity:
std::cout << "a<<b = " << a<<b << std::endl;
// What we probably meant:
std::cout << "a<<b = " << (a<<b) << std::endl;
return 0;
}which outputs:
a<<b = 57
a<<b = 640
Program ended with exit code: 0
You can output null-terminated strings, from a string literal, or using references to a character array, or a pointer to a string literal:
//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
std::cout << "Hello world!" << std::endl;
char buffer[] = "Bonjour, le monde!";
std::cout << buffer << std::endl;
char *ptr = "Wilkommen in das Weld!";
std::cout << ptr << std::endl;
const char *ptr2 = "Ciao mondo!";
std::cout << ptr2 << std::endl;
return 0;
}which outputs:
Hello world!
Bonjour, le monde!
Wilkommen in das Weld!
Ciao mondo!
Program ended with exit code: 0
But note the compile-time warning:
main.cpp:17:15 ISO C++11 does not allow conversion from string literal to 'char *'
Solved by adding a const:
const char *ptr2 = "Ciao mondo!";
char * vs void * Overloading for the << Operator
Note that there is a special overloading of the stream-insertion operator for char* types.
//
// main.cpp
// ostream
//
// Created by Bryan Higgs on 9/2/24.
//
#include <iostream>
int main(int argc, const char * argv[])
{
int i = 5;
int *int_ptr = &i;
std::cout << int_ptr << std::endl;
const char *char_ptr = "This is C++";
std::cout << char_ptr << std::endl;
std::cout << (void*) char_ptr << std::endl;
return 0;
}which outputs:
0x7ff7bfeff12c
This is C++
0x100003f30
Program ended with exit code: 0
Most pointer types output the contents of the pointer, not what it points to.
But char * outputs the string that the pointer points to. If you want the value of the pointer itself, you must explicitly cast the pointer to void *.
Note the addition of ‘const’ to avoid the compile-time warning.
The put Member Function
There is also the overloaded put member function defined for class ostream:
cout.put('A');
where the argument may be a char, unsigned char, or signed char.
Calls to put may be concatenated:
cout.put('A’).put('\n');
because the dot (.) operator, like the << operator, associates from left to right.