0
0
Cprogramming~10 mins

Using errno in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Using errno
Start program
Call function that may fail
Check if function failed
Yes
Read errno value
Use errno to identify error
Handle error or continue
End program
The program calls a function that might fail, then checks errno to find out what error happened, and handles it accordingly.
Execution Sample
C
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main() {
    FILE *f = fopen("nofile.txt", "r");
    if (!f) printf("Error: %s\n", strerror(errno));
    return 0;
}
This code tries to open a file that doesn't exist, then prints the error message using errno.
Execution Table
StepActionFunction Callerrno ValueOutput
1Start programNone0
2Call fopen("nofile.txt", "r")fopen2
3Check if fopen returned NULLNULL check2
4Read errno after failureerrno2
5Print error messagestrerror(errno)2Error: No such file or directory
6End programNone2
💡 fopen fails because file does not exist, errno is set to 2 (ENOENT)
Variable Tracker
VariableStartAfter fopenAfter checkFinal
fundefinedNULLNULLNULL
errno0222
Key Moments - 3 Insights
Why do we check if fopen returned NULL before reading errno?
Because errno is only meaningful if the function failed. Checking fopen's return (row 3) ensures we only read errno when an error happened.
Does errno reset automatically before each function call?
No, errno keeps its value until changed by a failing function. It should be set to 0 manually if you want to detect new errors reliably.
Why do we use strerror(errno) to print the error?
Because errno is just a number. strerror converts that number into a readable message (row 5 shows this).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of errno right after fopen fails?
A2
B0
C1
DUndefined
💡 Hint
Check Step 2 and Step 4 in the execution_table where errno is set after fopen fails.
At which step does the program print the error message?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the Output column in the execution_table to find when the error message appears.
If fopen succeeded, what would be the value of errno at Step 4?
A2
BIt would be unchanged from before
C0
DSome error code
💡 Hint
Refer to key_moments about errno not resetting automatically and only changing on failure.
Concept Snapshot
Using errno in C:
- Include <errno.h> and <string.h>
- Call a function that may fail (e.g., fopen)
- Check if function failed (e.g., fopen returns NULL)
- Read errno to get error code
- Use strerror(errno) to get readable message
- errno is set only on failure, not reset automatically
Full Transcript
This example shows how to use errno in C. The program tries to open a file that does not exist. fopen returns NULL to indicate failure. errno is set to 2, which means 'No such file or directory'. We check if fopen failed before reading errno. Then we use strerror(errno) to print a friendly error message. errno keeps its value until changed by another failing function. It does not reset automatically. This helps us understand how to detect and handle errors in C programs.