C++ Reference ยท Strings and I/O
C++ std::cout object
Reference for std::cout: the << operator, chaining, put, write, flush, endl versus newline, formatting flags like boolalpha, hex and fixed, and printing chars.
std::cout (header <iostream>) is the global std::ostream connected to the program's standard output. You send values to it with the insertion operator <<. There is an overload for every built-in type, for std::string, std::string_view, C strings and many library types, and you can add overloads for your own.
Each << returns the stream, so several outputs chain in one statement. Output is buffered: characters collect in memory and reach the terminal when the buffer fills, when the stream is flushed or when the program ends normally.
#include <iostream> int main() { std::cout << "Total: " << 3 * 4 << '\n'; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
extern std::ostream cout; std::ostream& operator<<(std::ostream& os, const T& value); // many overloads std::ostream& put(char c); std::ostream& write(const char* s, std::streamsize count); std::ostream& flush();
| Name | Description |
|---|---|
value |
The thing to print. Integers, floating point, bool, char, C strings, pointers, std::string, std::string_view, std::error_code, std::bitset, std::complex and more. |
c |
A single character for put. |
s, count |
A character array and the number of characters to write with write. |
Returns: all of them return a reference to std::cout, which enables chaining.
Errors / undefined behavior: writing a null const char* is undefined behavior. A failed write sets badbit (check with cout.fail()); exceptions are not thrown unless you enable them with cout.exceptions(...). Operator precedence can surprise: cout << a & b does not do what it looks like, so parenthesize cout << (a & b).
Usage notes
- Flush rules.
coutis tied tocin(it flushes before a read) andcerrflushes it before writing.std::endlis'\n'plus a flush;std::flushflushes alone. Prefer'\n'in loops. - Sticky versus one-shot.
std::hex,std::fixed,std::setprecision,std::boolalphaandstd::leftstay in effect until changed.std::setwapplies to the next output only. - Default float format prints up to 6 significant digits, so
3.14159265shows as3.14159. Usesetprecisionorstd::formatwhen you need control. - Pointers. A
const char*prints as text; other pointers print as an address. Cast toconst void*to see the address of achararray. charquirks.signed charandunsigned char(includinguint8_t) print as characters; add unary+or cast tointfor the number.- Thread safety. Concurrent
<<calls oncoutdo not cause data races (with default synchronization) but their output may interleave. Build the whole line first, or usestd::println, orstd::osyncstream. - Redirection. Standard output may be a file or pipe, where buffering is bigger and output order relative to
cerris no longer guaranteed. - Newer alternative.
std::print/println(C++23) are faster in many cases and need no manipulators.
Related
- iostream overview,
<iomanip>,std::print,std::cin - Lessons: Output, iostreams