#include
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.
我认为这种意外的结果是由于打印unsigned long long int 。 你如何printf()一个unsigned long long int ?
#1楼
使用VS2005将其编译为x64:
%llu运作良好。
#2楼
对于使用MSVS的很长一段时间(或__int64),应该使用%I64d:
__int64 a;
time_t b;
...
fprintf(outFile,"%I64d,%I64d\n",a,b); //I is capital i
#3楼
将ll(el-el)long-long修饰符与u(无符号)转换一起使用。 (在Windows,GNU中运行)。
printf("%llu", 285212672);
#4楼
非标准的东西总是很奇怪:)
对于GNU下的long long部分,它是L , ll或q
和windows下我相信这是ll只
#5楼
好吧,一种方法是使用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编译器。