Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Using errno for Error Reporting | Handling Runtime and System Errors
C Defensive Programming and Error Handling

bookUsing errno for Error Reporting

When you work with C programs that interact with the system or perform input/output operations, you may encounter situations where a function fails. The C standard library uses a global variable called errno to provide more information about such errors. The errno variable is set by library functions when an error occurs, allowing you to determine the specific cause of the failure. For instance, if you try to open a file that does not exist using fopen, the function returns NULL and sets errno to a value that indicates the reason for the error. You can then use the strerror function to convert the errno value into a human-readable error message, which is very useful for debugging and reporting errors to users.

main.c

main.c

copy
123456789101112
#include <stdio.h> #include <errno.h> #include <string.h> int main() { FILE *file = fopen("nonexistent.txt", "r"); if (file == NULL) printf("Error opening file: %s\n", strerror(errno)); else fclose(file); return 0; }
question mark

What is the purpose of errno in C?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 12.5

bookUsing errno for Error Reporting

Свайпніть щоб показати меню

When you work with C programs that interact with the system or perform input/output operations, you may encounter situations where a function fails. The C standard library uses a global variable called errno to provide more information about such errors. The errno variable is set by library functions when an error occurs, allowing you to determine the specific cause of the failure. For instance, if you try to open a file that does not exist using fopen, the function returns NULL and sets errno to a value that indicates the reason for the error. You can then use the strerror function to convert the errno value into a human-readable error message, which is very useful for debugging and reporting errors to users.

main.c

main.c

copy
123456789101112
#include <stdio.h> #include <errno.h> #include <string.h> int main() { FILE *file = fopen("nonexistent.txt", "r"); if (file == NULL) printf("Error opening file: %s\n", strerror(errno)); else fclose(file); return 0; }
question mark

What is the purpose of errno in C?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
some-alt