0
0
Cprogramming~10 mins

perror and strerror functions - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - perror and strerror functions
Error Occurs
Call perror(msg)
Prints: msg + error description
OR
Call strerror(errno)
Returns error description string
Use returned string for custom output
When an error happens, perror prints a message plus the error text. strerror returns the error text as a string to use in your code.
Execution Sample
C
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main() {
  FILE *f = fopen("nofile.txt", "r");
  if (!f) perror("File open error");
  return 0;
}
This code tries to open a missing file and uses perror to print the error message.
Execution Table
StepActionerrno ValueFunction CalledOutput
1Try to open 'nofile.txt' for reading2 (ENOENT)fopenNULL returned (file not found)
2Check if fopen returned NULL2if conditionTrue, enter error handling
3Call perror("File open error")2perrorFile open error: No such file or directory
4Program ends2return 0No output
💡 fopen fails, errno set to 2 (file not found), perror prints message, program ends
Variable Tracker
VariableStartAfter fopenAfter perrorFinal
fundefinedNULLNULLNULL
errno0222
Key Moments - 3 Insights
Why does perror print both my message and the error description?
perror automatically appends the system error message for the current errno after your message, as shown in step 3 of the execution_table.
What does strerror(errno) return and how is it different from perror?
strerror(errno) returns a string describing the error but does not print it. You can use it to build custom messages, unlike perror which prints directly (see concept_flow).
Why is errno set after fopen fails?
When fopen fails, it sets errno to indicate the error cause (like file not found). This is why perror and strerror can show the correct message (see step 1 and 2 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of errno after fopen fails?
A1
B2
C0
DUndefined
💡 Hint
Check step 1 and 2 in the execution_table where errno is shown after fopen fails.
At which step does perror print the error message?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Function Called' and 'Output' columns in the execution_table.
If we replace perror with printf("Error: %s\n", strerror(errno)), what changes in output?
AOutput will miss the custom message prefix
BOutput will be the same
COutput will include extra system info
DNo output will be printed
💡 Hint
Refer to the concept_flow and key_moments about how perror and strerror differ in output.
Concept Snapshot
perror(msg): prints msg + system error text for errno
strerror(errno): returns error text string for errno
Use perror for quick error print
Use strerror for custom error handling
errno holds last error code set by system calls
Full Transcript
When a system call or library function fails, it sets a global variable called errno to a number representing the error. The perror function prints a message you give it, followed by a colon and the system's description of that error number. The strerror function returns the error description as a string, so you can use it in your own messages. In the example, fopen tries to open a file that does not exist, so it returns NULL and sets errno to 2, meaning 'No such file or directory'. Then perror prints 'File open error: No such file or directory'. This helps you understand what went wrong quickly. strerror can be used if you want to build your own error messages instead of printing directly.