In C++, input and output (I/O) operations are performed using the standard input/output library, which is defined in the <iostream> header file.

The basic I/O operations in C++ are performed using the following four standard streams:

  • cin: This is the standard input stream, and is used to read data from the keyboard. It is associated with the operator >>.
  • cout: This is the standard output stream, and is used to write data to the screen. It is associated with the operator <<.
  • cerr: This is the standard error stream, and is used to write error messages to the screen. It is also associated with the operator <<.
  • clog: This is the standard log stream and is used to write log messages to the screen. It is also associated with the operator <<.

Here’s an example of a simple program that uses cin and cout to read an integer from the keyboard and write it to the screen:

#include <iostream>

int main() {
    int x;
    std::cout << "Enter an integer: ";
    std::cin >> x;
    std::cout << "You entered: " << x << endl;
    return 0;
}

You can also use the std namespace to avoid writing the std:: prefix for each standard library function. Here’s an example:

#include <iostream>
using namespace std;

int main() {
    int x;
    cout << "Enter an integer: ";
    cin >> x;
    cout << "You entered: " << x << endl;
    return 0;
}

Also, you could use the #include <bits/stdc++.h> header, it includes all the headers of the standard library, so you don’t have to include each one separately.

It is important to remember that the input operation will read until it finds a whitespace character, so if you want to read a string of characters you need to use the getline() function or the >> operator with a string variable.

Written by

’m Nilesh, a Software Development Engineer with 2+ years of experience, specializing in Go, JavaScript, Python, Docker, Kubernetes, Git, Jenkins, microservices, and system design (LLD/HLD), backed by a strong foundation in data structures and algorithms. Alongside my engineering journey, I bring 4+ years of hands-on experience in SEO, where I’ve worked extensively on content strategy, keyword research, technical SEO, and organic growth, helping products and businesses scale efficiently by aligning solid technology with search-driven performance.