title: Example of strerror_r
body: <p>I'm a newbie in error handling; in my code I need to test the returned value of a function and to print the error's description if an error happens.</p>
  <p>In order to keep the code thread-safe I have to use strerror_r, but I have some difficult to use it. In the following code the error number 22 happens (ret_setschedparam is 22). How can I print the description of the error number 22, i.e. &quot;Invalid argument&quot;, by using the <a href="https://man7.org/linux/man-pages/man3/strerror.3.html" rel="nofollow noreferrer">strerror_r</a>?</p>
  <p>I think that this prototype should be the right strerror_r I need:</p>
  <pre><code>char *strerror_r(int errnum, char *buf, size_t buflen);
  </code></pre>
  <hr />
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;sched.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  
  void *task();
  
  int main()
  {
  pthread_attr_t attr;
  struct sched_param prio;
  pthread_t tid;
  int ret_create;
  int ret_setschedparam;
  int ret_getschedparam;
  int ret_join;
  char *buf_setschedparam;
  size_t size_setschedparam = 1024;
  
  
  
   pthread_attr_init(&amp;attr);
  
   prio.sched_priority = 12;
   ret_setschedparam = pthread_attr_setschedparam(&amp;attr, &amp;prio);
   if (ret_setschedparam != 0) {
    printf(&quot;Errore numero (pthread_attr_setschedparam): %s\n&quot;, strerror_r(errno, buf_setschedparam, size_setschedparam));
    exit(EXIT_FAILURE);
    }
      
   ret_create = pthread_create(&amp;tid, &amp;attr, task, NULL);
   printf(&quot;%d %d\n&quot;, ret_create, EPERM);
   if (ret_create != 0) {
    printf(&quot;Errore numero (pthread_create): %d\n&quot;, ret_create);
  
    exit(EXIT_FAILURE);
    }
  
   ret_getschedparam = pthread_attr_getschedparam(&amp;attr, &amp;prio);
   if (ret_getschedparam != 0) {
    printf(&quot;Errore numero (pthread_attr_getschedparam): %d\n&quot;, ret_getschedparam);
    exit(EXIT_FAILURE);
    }
  
   printf(&quot;Livello di priorità del thread: %d\n&quot;, prio.sched_priority);
  
   ret_join = pthread_join(tid, NULL);
   if (ret_join != 0) {
    printf(&quot;Errore numero (pthread_join): %d\n&quot;, ret_join);
    exit(EXIT_FAILURE);
    }
  
   return(0);
  }
  
  
  void *task()
  {
   printf(&quot;I am a simple thread.\n&quot;);
   pthread_exit(NULL);
  }
  </code></pre>
  <p>The compiler gives me an error: it said that the output of strerror_r is an int, not a char.</p>