简体   繁体   中英

strtoull “seems” to return a wrong value

Basically I am trying to convert hex strings into unsigned long long values using strtoull . Here's the simple code

#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;
}

I expect val to print 8004298c42 but it prints 4298c42 . The 8 is being chopped off. I tried without the 0x and without the ULL too. But still the same result. Just to make sure that I am not missing out on some trivial printf format specifier thing, I even checked the contents of val . Still no use( ie Success was not printed )

I think I am missing out something trivial but don't know what!

The function strtoull() is declared in <stdlib.h> , not <string.h> , so the compiler thinks it returns an int , not an unsigned long long .

Cure: make sure that all functions are declared before they are used. If you use gcc , then you should consider some combination of -Wall , -Wextra , -Wstrict-prototypes , -Wmissing-prototypes , -Wold-style-declaration , -Wold-style-definition and -Werror .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM