How To Print Error Number In C
Contents |
Description Since so many functions return -1 on
Perror Example
error and set the value of the variable strerror example errno to be some number, it would sure be nice if you
What Is "errno"
could easily print that in a form that made sense to you. Mercifully, perror() does that. If you want perror errno more description to be printed before the error, you can point the parameter s to it (or you can leave s as NULL and nothing additional will be printed.) In a nutshell, this function takes errno values, like c error function ECONNRESET, and prints them nicely, like "Connection reset by peer." The function strerror() is very similar to perror(), except it returns a pointer to the error message string for a given value (you usually pass in the variable errno.) Return Value strerror() returns a pointer to the error message string. Example int s; s = socket(PF_INET, SOCK_STREAM, 0); if (s == -1) { // some error has occurred // prints "socket error: " + the error message: perror("socket error"); } // similarly: if (listen(s, 10) == -1) { // this prints "an error: " + the error message from errno: printf("an error: %s\n", strerror(errno)); } See Also errno <
of a library call. The functions strerror and perror give you the standard error message for a given error code; the variable class="nolinebreak">program_invocation_short_nameDescribe Iterative And Concurrent Server ?
Errno Undeclared
AC-Unsafe mem | See POSIX Safety Concepts. The strerror function maps the error code (see Checking for Errors) specified by the errnum argument to a descriptive error message string. http://beej.us/guide/bgnet/output/html/multipage/perrorman.html The return value is a pointer to this string. The value errnum normally comes from the variable errno. You should not modify the string returned by strerror. Also, if you make subsequent calls to strerror, the string might be overwritten. (But it’s guaranteed that no library function ever calls strerror behind your back.) The function strerror is declared in string.h. http://www.gnu.org/s/libc/manual/html_node/Error-Messages.html Function: char * strerror_r (int errnum, char *buf, size_t n) Preliminary: | MT-Safe | AS-Unsafe i18n | AC-Unsafe | See POSIX Safety Concepts. The strerror_r function works like strerror but instead of returning the error message in a statically allocated buffer shared by all threads in the process, it returns a private copy for the thread. This might be either some permanent global data or a message string in the user supplied buffer starting at buf with the length of n bytes. At most n characters are written (including the NUL byte) so it is up to the user to select a buffer large enough. This function should always be used in multi-threaded programs since there is no way to guarantee the string returned by strerror really belongs to the last call of the current thread. The function strerror_r is a GNU extension and it is declared in string.h. Function: void perror (const char *message) Preliminary: | MT-Safe race:stderr | AS-Unsafe corrupt i18n heap lock | AC-Unsafe corrupt lock mem fd | See POSIX Safety Concepts
(see feature_test_macros(7)): sys_errlist, sys_nerr: _BSD_SOURCE Description The routine perror() produces a message on the standard error output, describing the last error https://linux.die.net/man/3/perror encountered during a call to a system or library function. First (if s is not NULL and *s is not a null byte ('\0')) the argument string http://www.cplusplus.com/reference/cstring/strerror/ s is printed, followed by a colon and a blank. Then the message and a new-line. To be of most use, the argument string should include the how to name of the function that incurred the error. The error number is taken from the external variable errno, which is set when errors occur but not cleared when successful calls are made. The global error list sys_errlist[] indexed by errno can be used to obtain the error message without the newline. The largest message number how to print provided in the table is sys_nerr-1. Be careful when directly accessing this list because new error values may not have been added to sys_errlist[]. The use of sys_errlist[] is nowadays deprecated. When a system call fails, it usually returns -1 and sets the variable errno to a value describing what went wrong. (These values can be found in
if set to errno by a function of the library. The returned pointer points to a statically allocated string, which shall not be modified by the program. Further calls to this function may overwrite its content (particular library implementations are not required to avoid data races). The error strings produced by strerror may be specific to each system and library implementation. Parameters errnum Error number. Return Value A pointer to the error string describing error errnum. Example 1
2
3
4
5
6
7
8
9
10
11
12
13
/* strerror example : error list */ #include