cstrtol

Why does function `strtoll` gives a wrong value and set errno to 34?


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.


Solution

  • 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.