#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.
我假设这个意外的结果是由于打印unsigned long long int
造成的。How do you printf()
an unsigned long long int
发布于 2008-08-05 21:02:35
将ll (el-el) long-long修饰符与u(无符号)转换一起使用。(适用于windows、GNU)。
printf("%llu", 285212672);
发布于 2008-08-12 11:07:50
您可能希望尝试使用为您提供int32_t
、int64_t
、uint64_t
等类型的inttypes.h库。然后,您可以使用其宏,例如:
uint64_t x;
uint32_t y;
printf("x: %"PRId64", y: %"PRId32"\n", x, y);
这“保证”不会给你带来与long
、unsigned long long
等相同的麻烦,因为你不必猜测每种数据类型有多少位。
发布于 2015-05-14 01:48:33
用于int
的%d
-->
用于unsigned int
的%u
-->
用于long int
或long
的%ld
-->
%lu
-->适用于unsigned long int
、long unsigned int
或unsigned long
用于long long int
或long long
的%lld
-->
用于unsigned long long int
或unsigned long long
的%llu
-->
https://stackoverflow.com/questions/2844
复制相似问题