Page 156 - C-Language
P. 156

Chapter 21: Error handling




        Syntax



            •  #include <errno.h>
            •  int errno; /* implementation defined */
            •  #include <string.h>
            •  char *strerror(int errnum);
            •  #include <stdio.h>
            •  void perror(const char *s);


        Remarks



        Have in mind that errno is not necessarily a variable but that the syntax is only an indication how it
        might been declared. On many modern systems with thread interfaces errno is some macro that
        resolves to an object that is local to the current thread.


        Examples


        errno



        When a standard library function fails, it often sets errno to the appropriate error code. The C
        standard requires at least 3 values for errno be set:


          Value       Meaning

          EDOM        Domain error


          ERANGE      Range error

          EILSEQ      Illegal multi-byte character sequence



        strerror


        If perror is not flexible enough, you may obtain a user-readable error description by calling
        strerror from <string.h>.


         int main(int argc, char *argv[])
         {
             FILE *fout;
             int last_error = 0;

             if ((fout = fopen(argv[1], "w")) == NULL) {
                 last_error = errno;
                  /* reset errno and continue */
                  errno = 0;



        https://riptutorial.com/                                                                             132
   151   152   153   154   155   156   157   158   159   160   161