簡體   English   中英

unsigned long long int output 不工作

[英]unsigned long long int output not working

當給出的數字大於 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

——但它仍然在 unsigned long long int 的憤怒中......??????!?!

我也不明白為什么這條線不起作用

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

這個命令已經失敗,輸入 1 並給出 -1。(我猜是返回錯誤?)

謝謝你的幫助

1)節省時間,啟用所有警告

2) 使用匹配說明符u

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM