YouTip LogoYouTip

C Error Handling

# C Error Handling The C language does not provide direct support for error handling, but as a systems programming language, it allows you to access low-level data through return values. When an error occurs, most C or UNIX function calls return 1 or NULL, and simultaneously set an error code **errno**, which is a global variable indicating that an error occurred during the function call. You can find various error codes in the errno.h header file. Therefore, C programmers can check the return values and then decide which appropriate action to take based on those values. It is a good programming practice for developers to set errno to 0 during program initialization. A value of 0 indicates that there are no errors in the program. ## errno, perror(), and strerror() The C language provides the **perror()** and **strerror()** functions to display text messages related to **errno**. * The **perror()** function displays the string you pass to it, followed by a colon, a space, and a textual representation of the current errno value. * The **strerror()** function returns a pointer to a textual representation of the current errno value. Let's simulate an error condition by trying to open a non-existent file. You can use multiple ways to output error messages; here we use functions to demonstrate the usage. Also, note that you should use the **stderr** file stream to output all errors. ## Example #include#include#includeextern int errno ; int main(){FILE * pf; int errnum; pf = fopen("unexist.txt", "rb"); if(pf == NULL){errnum = errno; fprintf(stderr, "Error Number: %dn", errno); perror("Error printed by perror"); fprintf(stderr, "Error opening file: %sn", strerror(errnum)); }else{fclose(pf); }return 0; } When the above code is compiled and executed, it produces the following result: Error Number: 2 Error printed by perror: No such file or directory Error opening file: No such file or directory ## Division by Zero Error When performing division operations, if you do not check whether the divisor is zero, it will lead to a runtime error. To avoid this situation, the following code checks if the divisor is zero before performing the division: ## Example #include#includeint main(){int dividend = 20; int divisor = 0; int quotient; if(divisor == 0){fprintf(stderr, "Division by zero! Exiting...n"); exit(-1); }quotient = dividend / divisor; fprintf(stderr, "Value of quotient : %dn", quotient); exit(0); } When the above code is compiled and executed, it produces the following result: Division by zero! Exiting... ## Program Exit Status Typically, when a program completes an operation successfully and exits normally, it exits with the value EXIT_SUCCESS. Here, EXIT_SUCCESS is a macro defined as 0. If there is an error condition in the program, when you exit the program, it exits with the status value EXIT_FAILURE, defined as -1. Therefore, the program above can be written as: ## Example #include#includeint main(){int dividend = 20; int divisor = 5; int quotient; if(divisor == 0){fprintf(stderr, "Division by zero! Exiting...n"); exit(EXIT_FAILURE); }quotient = dividend / divisor; fprintf(stderr, "Value of quotient : %dn", quotient); exit(EXIT_SUCCESS); } When the above code is compiled and executed, it produces the following result: Value of quotient : 4 AI is thinking... [](#)(#) (#)[](#) [Volcengine Coding Plan supports mainstream large models like Doubao, GLM, DeepSeek, Kimi, MiniMax, etc., officially supplied, stable and reliable. Configuration Guide Β₯9.9/month Activate Now](https://maas.xfyun.cn/modelSquare?ch=maas_lm_l2E)
← Php Coalescing OperatorPhp Scalar Return Type β†’