title: C - string to uint64_t casting with error handling
body: <p>This program is to cast strings to <code>uint64_t</code> types and get the errors if any.</p>
  
  <p>It should output, in this case, 2 errors (overflow and negative number), none of which appear. Also, it doesn't properly cast one of the strings.</p>
  
  <p>Any help would be appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;inttypes.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;limits.h&gt;
  
  int func(const char *buff) {
      char *end;
      int si;
  
      errno = 0;
  
      const uint64_t sl = strtoull(buff, &amp;end, 10);
  
      if (end == buff) {
      fprintf(stderr, "%s: not a decimal number\n", buff);
      } else if ('\0' != *end) {
      fprintf(stderr, "%s: extra characters at end of input: %s\n", buff, end);
      } else if ((sl &lt; 0 || ULONG_MAX == sl) &amp;&amp; ERANGE == errno) {
      fprintf(stderr, "%s out of range of type uint64_t\n", buff);
      } else if (sl &gt; ULONG_MAX) {
      fprintf(stderr, "%ld greater than ULONG_MAX\n", sl);
      } else if (sl &lt; 0) {
       fprintf(stderr, "%ld negative\n", sl);
      } else {
      si = (int)sl;
      }
  
      return si;
  }
  
  int main()
  {
  
      printf("%d\n", func("123456789123465789"));
      printf("%d\n", func("123456789123465789123465789"));
      printf("%d\n", func("-1"));
  
  
      return 0;
  }
  </code></pre>