C errno.h
In this page:
#include <errno.h>
errno = 0;
function_that_may_fail();
if (errno != 0) {
perror("message");
}
What is errno?
errno is a global integer variable that C library functions set to a specific error code when something goes wrong internally, giving your program a standardized way to find out what kind of failure just occurred instead of just knowing that a function returned an error value.
Example: What is errno?
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <errno.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
errno = 0;
sqrt(-1.0);
// Print formatted text to the screen
printf("%d", errno != 0);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Triggering Math Errors
Certain math.h functions set errno to EDOM when you pass an argument outside the mathematically valid domain (like sqrt() of a negative number) or to ERANGE when the correct result would be too large or small to represent, letting you distinguish a bad input from an overflowed result.
Example: Triggering Math Errors
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <errno.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
errno = 0;
sqrt(-1.0);
// Print formatted text to the screen
printf("%d", errno == EDOM);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Printing Error Messages
strerror() takes an errno-style integer code and returns a pointer to a human-readable string describing what that code means, which is far more useful for debugging or logging than printing the raw numeric error code by itself.
Example: Printing Error Messages
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <errno.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
errno = ENOENT;
// Print formatted text to the screen
printf("%s", strerror(errno));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using perror()
perror() prints a message you supply, followed by a colon and the description of whatever error is currently stored in errno, combining your own context ('failed to open file') with the system's explanation ('No such file or directory') in one line.
Example: Using perror()
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <errno.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("nonexistent.txt", "r");
// Check whether fp == NULL
if (fp == NULL) {
perror("failed to open file");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Clearing errno
C library functions never reset errno back to 0 on success, they only ever set it on failure, so a stale error code from a previous call can linger and mislead you.
Setting errno = 0 immediately before an operation you want to check is the reliable way to detect whether that specific call actually failed.
Example: Clearing errno
// Include standard input/output (printf, scanf)
#include <stdio.h>
#include <errno.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
errno = 0;
sqrt(4.0);
// Print formatted text to the screen
printf("%d", errno);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Reading
errnoafter another function call has changed it, so the code no longer matches the failing function. - Forgetting
#include <errno.h>before usingerrno. - Not setting
errnoto 0 before the call, so an old error value is mistaken for a new one.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions