How To Write Error Messages In C
Contents |
of a library call. The functions strerror and perror give you the standard error message for a given error code; c error function the variable program_invocation_short_name gives you convenient access to the name
C Error Codes
of the program that encountered the error. Function: char * strerror (int errnum) Preliminary: | MT-Unsafe
C Error Handling Best Practices
race:strerror | AS-Unsafe heap i18n | AC-Unsafe mem | See POSIX Safety Concepts. The strerror function maps the error code (see Checking for Errors) specified by the errnum
Error.h C
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 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 c stderr 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 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 cha
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 About Us Learn more about Stack c programming error codes Overflow the company Business Learn more about hiring developers or posting ads with us error in c program Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a fprintf stderr c community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Printing error messages up vote 3 down vote favorite 2 I am just wondering what http://www.gnu.org/s/libc/manual/html_node/Error-Messages.html 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"); } } However, I don't think this perfect and want to do something much better. Maybe http://stackoverflow.com/questions/1679117/printing-error-messages 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 69686244395 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
there are ways to do error handling. Of course the programmer needs to prevent errors during coding and should always test the return values of functions called by the program. A lot of C function calls https://www.codingunit.com/c-tutorial-error-handling-exception-handling return a -1 or NULL in case of an error, so quick test on these return values are easily done with for instance an ‘if statement’. In previous tutorials we already mention that this behavior (returning http://www.cplusplus.com/reference/cstdio/perror/ numbers to indicate an error) is also used in Unix or Linux like operating systems. For instance if a program successful ends the return value of the program is zero. If the program ends with an error c error usually a number larger than zero is returned (for example 1). (With command ‘echo $?’ on the command line you can display the return code of a program that has previously run). So the one thing you need to remember is that you (the programmer) are responsible for error handling. You’re the person that needs to make sure that a program will gracefully terminate and not just CRASH unexpectedly! It is you that need how to write to take appropriate action depending on the return values of function calls. Global Variable errno The global variable errno is used by C functions and this integer is set if there is an error during the function call. To make use of errno you need to include errno.h and you need to call ‘extern int errno;’ Let us take a look at an example: #include
optionally preceding it with the custom message specified in str. errno is an integral variable whose value describes the error condition or diagnostic information produced by a call to a library function (any function of the C standard library may set a value for errno, even if not explicitly specified in this reference, and even if no error happened), see errno for more info. The error message produced by perror is platform-depend. If the parameter str is not a null pointer, str is printed followed by a colon (:) and a space. Then, whether str was a null pointer or not, the generated error description is printed followed by a newline character ('\n'). perror should be called right after the error was produced, otherwise it can be overwritten by calls to other functions. Parameters. str C string containing a custom message to be printed before the error message itself. If it is a null pointer, no preceding custom message is printed, but the error message is still printed. By convention, the name of the application itself is generally used as parameter. Return Value none Example 1
2
3
4
5
6
7
8
9
10
11
12
13
/* perror example */ #include