strtoull"似乎"返回错误的值

Pav*_*ath 0 c printf strtoull

基本上我试图将十六进制字符串转换为unsigned long long values使用strtoull.这是简单的代码

#include <stdio.h>
#include <string.h>


int main ()
{
    unsigned long long val =0;

    //printf("sizeof(unsigned long long)=%d\n", sizeof(unsigned long long));

    val = strtoull("0x8004298c42ULL",NULL,16);

    printf("%llx\n", val);

    if ( val == 0x8004298c42ULL)
    {
        printf("Success\n");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望val打印,8004298c42但它打印4298c42.将8被砍掉.我试着不0x和没有ULL了.但结果仍然相同.为了确保我没有错过一些简单的printf格式说明符,我甚至检查了内容val.仍然没有用(即没有打印成功)

我想我错过了一些微不足道但却不知道的东西!

Jon*_*ler 5

该函数strtoull()是声明的<stdlib.h>,而不是<string.h>,所以编译器认为它返回一个int,而不是一个unsigned long long.

解决:确保在使用之前声明所有函数.如果使用gcc,那么你应该考虑的一些组合-Wall,-Wextra,-Wstrict-prototypes,-Wmissing-prototypes,-Wold-style-declaration,-Wold-style-definition-Werror.