C function "strtoull" failing

I have a code in which I am passing string "5368709120" to function strtoull() and it should had returned me number 5368709120 but instead it returns me 1073741824 which is incorrect.

What may be the possible cause of this and how to rectify it?

    typedef unsigned long long    ULL_Type;
    char *numStr = "5368709120";
    ULL_Type            num;

    num = strtoull(numStr, NULL, 10);

MY this looks familiar: C function "strtoull" failing

You are doing yourself and the people who help you a disservice by plastering your question all over the net.

Your problem is: read about how to print an unsigned long long - the printf format string.

1 Like

I am not printing the value instead storing it in some variable.
now the value returnd by strtoull is wrong.

Show us the code you used to determine that the value stored in num is 1073741824 rather than 5368709120! Decimal 5368709120 is hexadecimal 0x140000000 and decimal 1073741824 is 0x40000000. So, Jim's guess that you printed the value of num using something like:

printf("%lu\n", num);

sure seems to fit the symptoms of what you have described.
Please change:

    num = strtoull(numStr, NULL, 10);

in your code to:

    num = strtoull(numStr, NULL, 10);
    printf("%llu\n", num);

and let us know what it prints.

1 Like

I printed it in gdb

(gdb) p strtoull(numStr, 0, 10) $17 = 1073741824

I tried p/u and it still prints the same thing.

Check the return value from strtoull...it is better not to assume anything and make sure to include <stdlib.h> header file wherein strtoull is declared and post the entire code if it aint too big...

1 Like

Thanks all for the reply.

---------- Post updated at 08:28 PM ---------- Previous update was at 08:21 PM ----------

I am using llu only
printf("Integer of %s is %llu \n", numStr , num);

Output is
Integer of 5368709120 is 1073741824

---------- Post updated at 08:31 PM ---------- Previous update was at 08:28 PM ----------

Breakpoint hit at strtoull():

(gdb) 
435                     num = strtoull(numStr, NULL, 10);

(gdb) p num
$1 = 140737488347680

(gdb) x/s numStr
0x7fffffffe5a3: "5368709120"

(gdb) p strtoull(numStr, NULL, 10)
$2 = 1073741824

(gdb) p/u strtoull(numStr, NULL, 10)
$3 = 1073741824

Executing the statement:

(gdb) n

(gdb) p num
$4 = 1073741824

(gdb) p/u num
$5 = 1073741824

Note that when I decrease one character from string i.e. if I pass "536870912" then it shows proper output.

Intger of 536870912 is 536870912 

What platform are you on...OS/hardware 32 or 64 bits? and insert the statement below into your source...compile it run it and post its output here...

printf("sizeof ULL is %d bytes\n", sizeof(ULL_Type));
1 Like

I am using 64 bit version of suse.

# uname -m
x86_64

Output of printf is:

sizeof ULL is 8 bytes

There's something goofy in your program so post it here so others can look at it...as the max value of ULL "18446744073709551615" is way bigger than "5368709120" so im not sure how you are getting wrong results...

1 Like

This is the program.
I have made small changes to make it look clear

int main(int argc, char *argv[])
   {
    unsigned long long int intValue;

    if(atoi(argv[2]) == 1)
    {   
        intValue = strtoull((const char *)argv[1], 0, 10 );
    }
    else
    {
//  ...
    }

    printf("intValue of %s is %llu \n", argv[1], intValue);

    return 0;
    }   

Output is

# ./str32_new "5368709120" 1
intValue of 5368709120 is 1073741824 
# ./str32_new "536870912" 1
intValue of 536870912 is 536870912

My OS is 64 bit suse.

If you had posted a complete yet minimal, compilable program which reproduces the issue, it is likely that this thread would have been resolved long ago.

Regards,
Alister

---------- Post updated at 03:19 PM ---------- Previous update was at 02:54 PM ----------

jim_mcnamara and don_cragun in posts #2 and #4, respectively, correctly deduced from the nature of the numbers involved that a long long was left-truncated into an integer. However, their responses were handicapped by not having access to a complete program.

Had your original post been more forthcoming, either of them would have seen immediately that the truncation was not caused by a printf bug but by incomplete headers.

You need to include the correct headers so that the compiler doesn't default to implicit function prototypes which treat arguments and return values as integers.

What you are seeing is the result of strtoull's return value taken as an integer before assignment to intValue (as if there were a cast operation there). There are surely other bugs just waiting to bite.

Consult the manual pages of each library function to learn which headers must be included.
Also, you should enable compiler warnings; they would have pointed this out to you.

Regards,
Alister

1 Like

I had not included stdlib.h.
Including it resolves the issue.