What is C++ Standard Error Stream (cerr)?

C++Object Oriented ProgrammingProgramming

std::cerr is an object of class ostream that represents the standard error stream oriented to narrow characters (of type char). It corresponds to the C stream stderr. The standard error stream is a destination of characters determined by the environment. This destination may be shared by more than one standard object (such as cout or clog).

As an object of class ostream, characters can be written to it either as formatted data using the insertion operator (operator<<) or as unformatted data, using member functions such as write. The object is declared in the header <iostream> with external linkage and static duration: it lasts the entire duration of the program.

You can use this object to write to the screen. For example, if you want to write "Hello" to the screen, you'd write −

Example

Live Demo

#include<iostream>
int main() {
   std::cerr << "Hello";
   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

This will give the output −

Hello
raja
Published on 15-Feb-2018 11:25:55
Advertisements