← Back to C Course | Chapter 12: Advanced Topics | Lesson 14 of 20

C errno.h

errno is a shared note where C library functions leave an error code when something goes wrong, like a warning light. You can read it to find out why.
Syntax
c
#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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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()

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Reading errno after another function call has changed it, so the code no longer matches the failing function.
  2. Forgetting #include <errno.h> before using errno.
  3. Not setting errno to 0 before the call, so an old error value is mistaken for a new one.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.