There are two useful functions for library function error handling: perror and exit: 1. Error Report
The perror function reports errors in a simple and unified manner. Many library functions in ansi c, especially I/O functions, call the operating system to perform some work. But you need to know that there is a possibility of an error in the operating system at any time. For example, if a program tries to read a non-existing disk file, the operating system prompts "error !", Nothing else can be done. At this time, the library function will first save the error code that represents the specific cause of the Operation error to the global integer variable errono (in errno. h), and then it sends the error message to the user program.
The perror function only reports specific errors represented by errno to users. Its prototype is defined in stdio. h as follows:
Perror (* message );
If the message is not NULL and points to a non-NULL string, perror prints the message first, followed by a colon ":" and a space. Finally, the error cause information of the current errno is printed.
Note: When the database function is successfully executed, the errno value is not modified; The errno value is modified only when the function fails to be executed. This means that we cannot test errno to determine whether a function execution error has occurred. Check the value of errno only when the function is correct.
Ii. End execution
Another very useful function is exit, which is used to end the execution of a program. Its prototype is defined in stdlib. h as follows:
Exit (status );
The exit function returns the parameter status to the operating system, and the status indicates whether the program is completed normally. It returns the same value as the value in the main function. There are two predefined symbols about status: EXIT_SUCCESS and EXIT_FAILURE, which indicate the success and failure respectively.
The exit function is particularly useful when an error condition is found to prevent the program from continuing to execute. Generally, after calling the perror function, we call the exit function.
Note: The exit function never returns any value. When exit ends, the program disappears. Where can I return.