Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Error Handling and Robust Coding

Lesson 31 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

Writing Code that Doesn't Break

In a perfect world, users always type what we expect, and memory is infinite. In the real world, things fail. A great C programmer spends 20% of their time writing logic and 80% handling potential errors.

1. The errno Variable

When a standard C function (like opening a file or allocating memory) fails, it sets a global hidden variable called errno to a specific error number.

  • perror("Message"): Prints your custom message followed by a human-readable description of what went wrong (e.g., "File not found" or "Permission denied").
  • strerror(errno): Returns a string describing the error code so you can log it.

2. Common Runtime Crashes

  • Segmentation Fault: You tried to touch memory you don't own. (Usually a bad pointer).
  • Stack Overflow: Your recursion went too deep and ran out of stack space.
  • Bus Error: You tried to access memory in a way the hardware doesn't allow (misalignment).
  • Arithmetic Exception: Usually a "Division by Zero" error.

3. Logic Errors (The Silent Killers)

The program doesn't crash, but it produces the wrong output.

  • Off-by-one: Your loop runs 11 times instead of 10.
  • Assignment instead of Equality: Writing if (x = 5) instead of if (x == 5). The first one makes x equal to 5 and is ALWAYS true!

Debugging Best Practices

  1. Read the Compiler Warnings: Use the -Wall (Warn All) flag with GCC. If the compiler is complaining, don't ignore it!
  2. Print Debugging: The classic "Insert printf everywhere" method. printf("Reached line 42, val = %d\n", val);
  3. Use a Debugger (GDB): Learn to use a real debugger. It allows you to pause time, step through code line-by-line, and see exactly what is happening in RAM.
Use the assert(condition) macro from <assert.h>. It will instantly crash your program during development if a condition that should be true is actually false. It's like a tripwire for bugs!