cerr in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The cerr in C++ is a standard output stream for errors. This object is declared in the <iostream> header file. It is basically used to print some information or error on the console.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

What is cerr in C++?

The cerr is an object of class ostream, which is associated with the standard error output stream. It is used along with insertion operator(<<) to print the error and information on the console. Also, cerr is an unbuffered stream i.e. it prints the information to the console directly without storing it in buffer. The flush function is called every time before the print operation. To use the cerr in our program we need to import the iostream header file. In the cerr, here 'c' represents character.

Syntax of cerr in C++

The syntax of cerr is pretty simple. It is quite similar to the syntax of cout object.

Sharpen Your Fundamentals with Free Learning

Example

Output:

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

More Examples

Opening a File

Output:

Explanation:

  • In this case, the sample.txt file doesn't exist. So, else block is being executed which is printing some error string on the console.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Try Catch

Output:

Explanation:

  • Initially, we have defined an array and we are finding the size of that array.
  • In the try block, we have defined a condition to throw an error whenever the index is out of range.
  • In the catch block, we are catching the error and cerr is being used to print the error.

Conclusion

  • cerr is an object of ostream class.
  • It is declared in iostream header file.
  • It is used to print the information or error to the console.

See Also: