Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ iostream library overview

Overview of the C++ iostream library: the stream class hierarchy, cin, cout, cerr and clog, operators << and >>, buffering, sync_with_stdio and tie.

The iostream library is C++'s classic input and output framework. Instead of format strings it uses streams: objects that you write to with << and read from with >>. A stream hides where the characters go, so the same code can talk to the console, a file or a string in memory.

The header <iostream> declares four global stream objects: std::cin for standard input, std::cout for standard output, std::cerr for unbuffered error output and std::clog for buffered log output. Each also has a wide-character twin (wcin, wcout, wcerr, wclog).

C++
#include <iostream>
#include <string>

int main() {
    std::cout << "Your name? ";
    std::string name;
    std::cin >> name;
    std::cout << "Hello, " << name << "\n";
}

Try it

C++ Reading and writing with cin and cout

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.

C++ The stream class hierarchy

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.

The stream hierarchy

  • std::ios_base holds formatting flags, precision and width.
  • std::basic_ios adds the stream state (good, fail, eof, bad) and the connection to a buffer (rdbuf).
  • std::istream adds input (>>, get, getline, read, peek, ignore); std::ostream adds output (<<, put, write, flush); std::iostream has both.
  • std::ifstream, std::ofstream, std::fstream attach a file; std::istringstream, std::ostringstream, std::stringstream attach a string. See file streams and string streams.
  • Forward declarations of all of these are in <iosfwd>; <istream> and <ostream> define the classes without the global objects.

The four standard streams

Object Direction Buffering Typical use
std::cin input buffered reading user input, see std::cin
std::cout output buffered normal program output, see std::cout
std::cerr output (stderr) unbuffered error messages; flushes cout first because they are tied
std::clog output (stderr) buffered diagnostic logging

Usage notes

  • Operators chain because << and >> return the stream. cout << a << b is two calls.
  • Failures are silent. A bad read sets failbit and later reads do nothing until you clear it. Always test the stream (if (cin >> x)). See stream states.
  • '\n' versus std::endl. endl writes a newline and flushes. Flushing on every line is slow in loops, so prefer '\n' and flush only when needed.
  • std::ios::sync_with_stdio(false) stops iostream from staying in lockstep with C's stdio. It speeds up heavy input and output but you must not mix cin / cout with scanf / printf afterwards. Call it before any I/O.
  • cin.tie(nullptr) removes the automatic flush of cout before each input operation. Together with the line above it is the usual competitive-programming speed setup, but interactive prompts then may not appear before the program waits.
  • Locale. Streams use a locale for numbers; the default is the classic "C" locale, which prints 1234.5 and not 1.234,5.
  • Prefer <print> for new output code. std::println is type safe like streams, but formats a whole line in one call and has no sticky state. Streams remain the way to read input and the base for file and string I/O.
  • Customize. Give your class std::ostream& operator<<(std::ostream&, const T&) to print it with <<.