3

Here's my C code:

    char *ptr = "0xfff1342809062Cac";
    char *pEnd;
    long long value = strtoll(ptr, &pEnd, 0);
    printf("%lld\n", value);
    printf("errno: %d\n", errno);

I compiled it with gcc-8.3.0, and the output is:

9223372036854775807
errno: 34

I'm confused that strtoll gives an unexpected value and set errno to 34.

Sean
  • 1,055
  • 11
  • 10

1 Answers1

8

This behaviour is correct. On your system the maximum value for long long, i.e. LLONG_MAX is 9223372036854775807.

The value in your string is larger than this; and the specified behaviour if the value is out of range and too big is: return LLONG_MAX and errno is set to ERANGE (presumably 34 on your system).

Perhaps consider using strtoull and an unsigned long long return value , since that string would fit in that data type.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • 1
    In the posted code, the value of `errno` does not necessarily indicate why `strtoll()` may have failed. [Per the C standard](https://port70.net/~nsz/c/c11/n1570.html#7.21.6.3), the `printf()` function does no set `errno` on error, so `printf()` is [free to modify the value in `errno`](https://port70.net/~nsz/c/c11/n1570.html#7.5p3): "The value of `errno` may be set to nonzero by a library function call whether or not there is an error, provided the use of `errno` is not documented in the description of the function in this International Standard." – Andrew Henle Mar 23 '20 at 11:24