#include <stdio.h>
int main() {
unsigned long long int num = 285212672; //FYI: fits in 29 bits
int normalInt = 5;
printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);
return 0;
}
输出。
My number is 8 bytes wide and its value is 285212672l. A normal number is 0.
我认为这个意外的结果是由于打印了 "无符号长形int"。你如何printf()
一个`无符号长长的int'?
使用ll(el-el)long-long修饰符与u(无符号)转换。(在windows、GNU中有效)。
printf("%llu", 285212672);
那么,一种方法是用VS2008将其编译为x64版本
这正如你所期望的那样运行。
int normalInt = 5;
unsigned long long int num=285212672;
printf(
"My number is %d bytes wide and its value is %ul.
A normal number is %d \n",
sizeof(num),
num,
normalInt);
对于32位代码,我们需要使用正确的__int64格式指定器%I64u。因此,它变成了。
int normalInt = 5;
unsigned __int64 num=285212672;
printf(
"My number is %d bytes wide and its value is %I64u.
A normal number is %d",
sizeof(num),
num, normalInt);
这段代码对32位和64位的VS编译器都适用。