Last Updated : 15 Jul, 2025
In C++, cerr is the standard error stream used to output the errors. It is an instance of the ostream class and is un-buffered, so it is used when we need to display the error message immediately and does not store the error message to display later. The 'c' in cerr refers to "character" and 'err' means "error", Hence cerr means "character error".
Example:
CPP
#include <iostream>
using namespace std;
int main() {
// Printing error
cerr << "Welcome to GfG! :: cerr\n";
// Printing using cout
cout << "Welcome to GfG! :: cout";
return 0;
}
Error
Welcome to GfG! :: cerr Welcome to GfG! :: cout
Explanation: In this program, cerr is used to print an error message immediately to the console because it is unbuffered. The message "Welcome to GfG! :: cerr" is displayed without delay. On the other hand, cout is used for standard output, and the message "Welcome to GfG! :: cout" may be buffered and displayed after the program execution completes.
Syntax of cerrcerr is defined inside the <iostream> header file and is similar to the standard output stream.
Key Features of cerrcerr << "Error message";
The following are the key features of cerr object:
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
// Displaying error using cerr
if (n2 == 0) {
cerr << "Error: Division by zero!" << endl;
return 1;
}
cout << "Result: " << n1 / n2 << endl;
return 0;
}
Output
Enter two numbers: 10 0 Error: Division by zero!
Explanation: In this program, cerr is used to display an error message if the user tries to divide by zero. If n2 is zero, the message "Error: Division by zero!" is immediately printed to the console, signalling an error, and the program exits with a return value of 1.
Redirecting cerr to a FileAlthough cerr is typically used to output errors to the console, it can also be redirected to a file. This can be helpful when you need to log errors in a program.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// Creating ostream to a log file
ofstream errorLog("error_log.txt");
// Redirect cerr to the file
cerr.rdbuf(errorLog.rdbuf());
cerr << "This is an error message written to a file." << endl;
errorLog.close();
return 0;
}
In the file error_log.txt:
This is an error message written to a file.
C++ cerr - FAQs
Can cerr be used for normal output?How do I redirect cerr to a file?No, cerr is specifically for error output. Use cout for normal program output.
Is cerr buffered?You can redirect cerr to a file using the rdbuf() method, similar to how you redirect cout.
What happens if cerr fails?No, cerr is unbuffered, ensuring immediate display of error messages.
If cerr fails to output an error message, it typically means there is an issue with the output stream or the system environment.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4