以下代码在给出大于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);