Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

I tried researching the difference between cout, cerr and clog on the internet but couldn't find a perfect answer. I still am not clear on when to use which. Can anyone explain to me, through simple programs and illustrate a perfect situation on when to use which one?

I visited this site which shows a small program on cerr and clog, but the output obtained over there can also be obtained using cout. So, I'm confused over each one's exact use.

like image 992
Arlene Batada Avatar asked May 27 '13 12:05

Arlene Batada


People also ask

What is the difference between CERR and clog?

cerr and clog both are associated with the standard C error output stream stderr but the cerr is the unbuffered standard error stream whereas the clog is the buffered standard error stream.

What is CERR used for?

The cerr object in C++ is used to print error messages. It is defined in the iostream header file.

What is std :: clog?

std::clog. extern ostream clog; Standard output stream for logging. Object of class ostream that represents the standard logging stream oriented to narrow characters (of type char ). It corresponds, along with cerr , to the C stream stderr .

What does the CERR present?

Explanation: cerr is an object of class ostream that represents the standard error stream. It is associated with the cstdio stream stderr.


1 Answers

Generally you use std::cout for normal output, std::cerr for errors, and std::clog for "logging" (which can mean whatever you want it to mean).

The major difference is that std::cerr is not buffered like the other two.


In relation to the old C stdout and stderr, std::cout corresponds to stdout, while std::cerr and std::clog both corresponds to stderr (except that std::clog is buffered).

like image 55
Some programmer dude Avatar answered Sep 28 '22 06:09

Some programmer dude