LearnFrenzy
Sign in Take a free test
C

C Programming · Topic 22 of 23

Error Handling Questions and Concepts

  • 4 exercises
  • 40 questions
  • ~60 min
  • Answers explained

Interview Questions

Error Handling · Study material

Error Handling in C – Interview Questions

1. How are errors handled in C without exceptions?

Through return values (NULL, EOF, −1, error codes), errno for library functions, and program termination with exit or abort. setjmp/longjmp can jump out of nested calls.

2. What is errno?

A modifiable int lvalue (thread-local in modern libraries) declared in <errno.h>, set to a positive error number by failing library functions. It is never set to 0 by a library function.

3. Difference between perror and strerror?

perror(s) prints "s: message" to stderr; strerror(n) returns the message text for error number n.

4. What do EXIT_SUCCESS and EXIT_FAILURE mean?

Portable exit statuses from <stdlib.h>. EXIT_SUCCESS (like 0) means success; EXIT_FAILURE means failure.

5. What is the difference between exit() and abort()?

exit performs normal termination: atexit handlers, flushing and closing streams. abort raises SIGABRT and ends abnormally without them.

6. What happens to assert when NDEBUG is defined?

Each assert expands to nothing, so its expression is not even evaluated.

7. When would you use static_assert?

To check assumptions the compiler can evaluate: type sizes, struct layout, array lengths matching enum counts.

8. How does setjmp/longjmp work?

setjmp saves the execution context and returns 0; longjmp restores it, making setjmp return again with a non-zero value. Locals modified after setjmp must be volatile to have reliable values.

9. Why is atoi considered unsafe?

It has no way to report invalid input or overflow – out-of-range values are undefined behaviour. strtol reports both.

10. Is the goto statement acceptable for error handling?

Yes – forward jumps to a cleanup label are a widely accepted C idiom for releasing resources on every error path.

11. How do you detect integer overflow?

Check before the operation (e.g. a > INT_MAX − b) or use C23 ckd_add/ckd_sub/ckd_mul (or compiler builtins such as __builtin_add_overflow).

12. What may a signal handler safely do?

In portable C, assign to a volatile sig_atomic_t variable, call signal for the same signal, _Exit, abort or quick_exit.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C Programming

C programming is the language most computer science courses start with and the foundation of operating systems, embedded devices and compilers. This section helps you learn C properly – data types, operators, control statements, functions, arrays, strings, pointers, structures, memory allocation and files – and then prove it with hundreds of practice questions explained line by line.

Why it matters

  • C teaches how a computer really works: memory, addresses and how data is stored
  • Pointers and memory questions are a favourite in written tests and technical interviews
  • Once you know C, learning C++, Java and Python becomes much easier
  • Embedded systems, operating systems and many companies' coding rounds still use C

Who it is for

  • School students (Class 11–12 computer science) building a strong programming base
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, aptitude-cum-coding tests and technical interviews
  • Anyone preparing for GATE CS or government IT exams

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro tests
  • Technical interviews
  • GATE CS
  • University exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links