Printing Error Message In C
Contents |
of a library call. The functions strerror and perror give you the standard error message for a given error code; the variable program_invocation_short_name gives you convenient access to the name of the program c error codes that encountered the error. Function: char * strerror (int errnum) Preliminary: | MT-Unsafe race:strerror c error handling best practices | AS-Unsafe heap i18n | AC-Unsafe mem | See POSIX Safety Concepts. The strerror function maps the error code (see Checking for error.h c Errors) specified by the errnum argument to a descriptive error message string. The return value is a pointer to this string. The value errnum normally comes from the variable errno. You should not modify the c stderr 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. 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
C Programming Error Codes
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. This function prints an error message to the stream stderr; see Standard Streams. The orientation of stderr is not changed. If you call perror with a message that is either a null pointer or an empty string, perror just prints the error message corresponding to errno, adding a trailing newline. If you supply a non-null message argument, then perror prefixes its output with this string. It
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site
Fprintf Stderr C
About Us Learn more about Stack Overflow the company Business Learn more about error in c program hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join strerror in c the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Printing error messages up http://www.gnu.org/s/libc/manual/html_node/Error-Messages.html vote 3 down vote favorite 2 I am just wondering what is the best way to make custom print error functions. For example I have some #defines like this in header file: #define SOCKET_ERR 0 #define BIND_ERR 1 #define LISTEN_ERR 2 etc Then maybe using this like this: if(/*something has gone wrong with socket*/) { print_error(SOCKET_ERR); } print_error(int error) { if(error == 0) { printf("Socket failure\n"); http://stackoverflow.com/questions/1679117/printing-error-messages } } However, I don't think this perfect and want to do something much better. Maybe something a little bit more professional and maybe more scalable. Many thanks for any advice, c share|improve this question asked Nov 5 '09 at 8:30 ant2009 78686247396 1 Use 'fprintf(stderr, ...)' to report errors (or, at least, normally write to 'stderr' rather than 'stdout' - or write to a log file, or both log file and stderr). –Jonathan Leffler Nov 5 '09 at 8:51 add a comment| 3 Answers 3 active oldest votes up vote 3 down vote accepted You might consider using variadic functions for error reporting, they become so much more versatile. For instance #include
Description Since so many functions return -1 on http://beej.us/guide/bgnet/output/html/multipage/perrorman.html error and set the value of the variable errno to be some number, it would sure be nice if you could easily print that in a form that made sense to you. Mercifully, perror() does that. If you want in c 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 error in c 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 <