What are cin, cout and cerr streams in C++?



The cin, cout, cerr, and clog are streams that handle standard input and output stream objects, which are defined in an <iostream> header file.

Standard Output Stream (std::cout)

The cout is an object of class ostream that represents the standard output stream oriented to narrow characters (of type char). It corresponds to the C stream stdout. The standard output stream is the default destination of characters determined by the environment. This destination may be shared with more standard objects (such as cerr or clog).

Syntax

Here is the following syntax for cout in C++:

cout << variable_name;

Here, << is an insertion operator.

variable_name, is the value that will be displayed as output.

Example

#include <iostream>
using namespace std;

int main() {
    string name = "TutorialsPoint";
    int year = 2014;

    cout << "Name: " << name << endl;
    cout << "Year: " << year << endl;

    return 0;
}

Output

Name: TutorialsPoint
Year: 2014

Here, the cout takes the values stored in a variable and sends them to the screen using <<.

Standard Input Stream (std::cin)

The cin is an object of class istream that represents the standard input stream oriented to narrow characters (of type char). It corresponds to the C stream stdin. The standard input stream is a source of characters determined by the environment. It is generally assumed to be input from an external source, such as the keyboard or a file.

Syntax

Here is the following syntax for cin in C++:

cin >> variable_name;

Here, >> is an extraction operator.

variable_name is the variable where the input provided by the user will be stored

Example

#include <iostream>
using namespace std;

int main() {
    
    int age;
    cout << "Enter your age: ";
    cin >> age;
    
    cout << "You entered: " << age << endl;

    return 0;
}

Output

Enter your age: 33
You entered: 33

Standard Output Stream (std::cerr)

The std::cerr is an object in C++, which is used to output error messages to the standard error stream. Its main purpose is to display error messages or warnings to the user. It is connected to the stream buffer, which is part of stderr (standard error), a concept derived from C programming <cstdio>

Syntax

Here is the following syntax for cerr in C++:

cerr << expression;

Example

#include <iostream>
using namespace std;

int main() {
    int age = -2;

    if (age < 0) {
        cerr << "Error: Age cannot be negative!" << endl;
    } else {
        cout << "Your age is: " << age << endl;
    }

    return 0;
}

Output

Error: Age cannot be negative!

Note: All the objects declared in this header share a peculiar property - you can assume they are constructed before any static objects you define, in a translation unit that includes <iostream>. Equally, you can assume that these objects are not destroyed before the destructors for any such static objects you define. (The output streams are, however, flushed during program termination.) Therefore, you can safely read from or write to the standard streams before program startup and after program termination. You can use these stream objects as follows ?

C++ Example of cin, cout and cerr Streams

This example demonstrates the usages of cin, cout and cerr streams:

#include<iostream>
int main() {
   int my_int;
   std::cin >> my_int;
   std::cout << my_int;
   std::cerr << "An error message";
   return 0;
}

Then save this program to hello.cpp file. Finally, navigate to the saved location of this file in the terminal/cmd and compile it using:

$ g++ hello.cpp

Run it using:

$ ./a.out

Output

If you give it the input 15, this will give the output:

15 An error message
Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2025-05-06T19:07:14+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements