为什么函数`strtoll` 给出错误值并将errno 设置为34?

Sea*_*ean 3 c strtol

这是我的 C 代码:

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

我是用gcc-8.3.0编译的,输出是:

9223372036854775807
errno: 34
Run Code Online (Sandbox Code Playgroud)

我很困惑 strtoll 给出了一个意想不到的值并将 errno 设置为 34。

M.M*_*M.M 8

这种行为是正确的。在您的系统的最大价值long long,即LLONG_MAX9223372036854775807

您的字符串中的值大于此值;如果值超出范围并且太大,则指定的行为是:返回LLONG_MAXerrno设置为ERANGE(在您的系统上大概是 34)。

也许考虑使用strtoullunsigned long long返回值,因为该字符串适合该数据类型。