Landjalan
Landjalan

Reputation: 11

unsigned long long int output not working

the following code fails when giving in bigger numbers than 111111111111111111 (19 times 1).

although unsigned long long int should hold up to 18446744073709551615 (19 numbers), but 111111111111111111 (19 times 1) fails

#include <stdio.h>
#include <stdlib.h>
// included for sleep test after num printf
#include <unistd.h>
#include <limits.h>
#define MAX 2000000

unsigned long long int num;


unsigned long long int main(){
    for(;;){


        char buf[MAX];
        printf("                               : %llu\n", (unsigned long long int) ULONG_MAX);
        printf("enter max number : ");
        fgets(buf, MAX, stdin);

        num = strtoull(buf,NULL,10);

        printf("num holds number : %lld\n",num);
        printf("ULONG_MAX        : %lld\n\n\n\n", (unsigned long long int) ULLONG_MAX);
    }
    return num;

}

using 19 times 1 , i get --

num holds number : -7335632962598440505

-- but it is still in rage of unsigned long long int...??!?!?!?!

also i dont get why this line does not work

printf("ULONG_MAX        : %lld\n\n\n\n", (unsigned long long int) ULLONG_MAX);

this command already fails with input 1 and gives -1.(return of error i guess ?)

thanks for any help

Upvotes: 1

Views: 955

Answers (1)

chux
chux

Reputation: 153517

1) Save time, enable all warnings

2) Use the matching specifier u

unsigned long long int num;
....
// printf("num holds number : %lld\n",num);
//                            v
printf("num holds number : %llu\n",num);

Upvotes: 1

Related Questions