errno helps you find out what went wrong when a C function fails. It stores error codes so you can understand and fix problems.
Using errno in C
#include <errno.h> #include <stdio.h> #include <string.h> // errno is a global variable // Use perror() or strerror(errno) to print error messages
errno is set by system and library calls when an error happens.
Do not change errno yourself; just read it after a failure.
#include <errno.h> #include <stdio.h> #include <string.h> int main() { FILE *f = fopen("missing.txt", "r"); if (!f) { printf("Error opening file: %s\n", strerror(errno)); } return 0; }
#include <errno.h> #include <stdio.h> int main() { FILE *f = fopen("missing.txt", "r"); if (!f) { perror("fopen failed"); } return 0; }
This program tries to open a file that does not exist. It then prints the numeric error code, the error message string, and uses perror to show the error.
#include <errno.h> #include <stdio.h> #include <string.h> int main() { FILE *file = fopen("nonexistent.txt", "r"); if (!file) { printf("Failed to open file. errno = %d\n", errno); printf("Error message: %s\n", strerror(errno)); perror("perror output"); return 1; } fclose(file); return 0; }
errno is only valid right after a function fails; calling other functions may change it.
Always check the function's return value before using errno.
Different systems may have different error codes, but common ones like ENOENT (file not found) are standard.
errno stores error codes set by failed functions.
Use strerror(errno) or perror() to get readable error messages.
Check function results before reading errno.
Practice
errno in C programming?Solution
Step 1: Understand what
errnostoreserrnois a global variable that stores error codes set by system or library functions when they fail.Step 2: Differentiate from other options
It does not count errors, reset program state, or display messages automatically.Final Answer:
To store error codes set by system or library functions when they fail -> Option AQuick Check:
errno stores error codes = C [OK]
- Thinking errno counts errors
- Assuming errno resets program state
- Believing errno prints messages automatically
errno in a C program?Solution
Step 1: Identify the standard header for errno
The standard header file to useerrnois<errno.h>.Step 2: Verify other options
<error.h>,<err.h>, and<errors.h>are not standard headers forerrno.Final Answer:
#include <errno.h> -> Option BQuick Check:
errno header = <errno.h> [OK]
- Using wrong header files
- Forgetting to include errno.h
- Confusing with similar-sounding headers
nonexistent.txt does not exist?#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
FILE *f = fopen("nonexistent.txt", "r");
if (!f) {
printf("Error: %s\n", strerror(errno));
}
return 0;
}Solution
Step 1: Understand fopen failure and errno
Whenfopenfails to open a non-existing file for reading, it setserrnoto indicate the error, usuallyENOENTmeaning "No such file or directory".Step 2: Use strerror to get error message
strerror(errno)converts the error code to a readable string, which will be "No such file or directory".Final Answer:
Error: No such file or directory -> Option AQuick Check:
fopen missing file error = "No such file or directory" [OK]
- Assuming errno is zero without checking fopen result
- Confusing error messages for different errors
- Not including string.h for strerror
errno:#include <stdio.h>
#include <errno.h>
#include <string.h>
int main() {
errno = 0;
FILE *f = fopen("file.txt", "r");
if (f == NULL) {
perror("File open error");
}
if (errno != 0) {
printf("Error code: %d\n", errno);
}
return 0;
}Solution
Step 1: Analyze errno usage after fopen
errno should only be checked if the function indicates failure. Here, errno is checked regardless of fopen result, which can be misleading.Step 2: Confirm correct error checking pattern
The correct pattern is to check if fopen returned NULL before reading errno. This code checks errno even if fopen succeeded.Final Answer:
Checking errno after fopen without checking fopen result first -> Option CQuick Check:
Check function result before errno [OK]
- Reading errno without function failure
- Confusing perror and strerror usage
- Ignoring file close after fopen
errno and strerror to do this?A) FILE *f = fopen("data.txt", "r");
if (!f) {
printf("Error: %s\n", strerror(errno));
}
B) errno = 0;
FILE *f = fopen("data.txt", "r");
if (f == NULL) {
printf("Error: %s\n", strerror(errno));
}
C) FILE *f = fopen("data.txt", "r");
if (f == NULL) {
errno = 0;
printf("Error: %s\n", strerror(errno));
}
D) FILE *f = fopen("data.txt", "r");
if (f == NULL) {
perror("Error opening file");
}
Solution
Step 1: Check correct error handling with errno
Option D usesperror, which is the standard and simplest way to print a custom error message along with the error string corresponding toerrno. It automatically handleserrnocorrectly.Step 2: Identify issues in other options
Option D does not reseterrnobeforefopen, which is less safe. Option D resetserrnobeforefopenand checks failure, but it's generally unnecessary to reseterrnobefore calls that set it on failure. Option D resetserrnoafter failure, clearing the error code before printing, which is incorrect.Final Answer:
Option D -> Option DQuick Check:
Use perror for simple and correct error messages [OK]
- Resetting errno unnecessarily
- Resetting errno after failure
- Using strerror without valid errno
Ready to prove it?
Pass the test and this pattern moves unknown
