unsigned long long int输出不起作用

时间:2020-06-10 19:52:17

标签: c linux gcc long-integer unsigned

当给出的数字大于 111111111111111111(19乘1)。

尽管unsigned long long int最多可容纳18446744073709551615(19个数字),但111111111111111111(19乘以1)失败

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

}

使用19次1,我得到-

num holds number : -7335632962598440505

-但它仍然处于未签名的long long int范围之内... ??!?!!?!?!!

我也不明白为什么这行不起作用

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

此命令在输入1时已经失败并给出-1。(我猜是返回错误)

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

1)节省时间,启用所有警告

2)使用匹配的说明符u

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