1 2 3 4 5 6 7 8
| #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.
", sizeof(num), num, normalInt);
return 0;
} |
输出:
1
| 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?
将ll(el-el)long-long修饰符与u(无符号)转换一起使用。 (适用于Windows,GNU)。
1
| printf("%llu", 285212672); |
您可能想尝试使用为您提供类型的inttypes.h库
int32_t,int64_t,uint64_t等
然后,您可以使用其宏,例如:
1 2 3 4 5
| uint64_t x;
uint32_t y;
printf("x: %"PRId64", y: %"PRId32"
", x, y); |
这是"保证"不会给你带来与long,unsigned long long等相同的麻烦,因为你不必猜测每种数据类型中有多少位。
%d - > int
%u - > unsigned int
%ld - > long int
%lu - > unsigned long int
%lld - > long long int
%llu - > unsigned long long int
对于使用MSVS的long long(或__int64),您应该使用%I64d:
1 2 3 4 5
| __int64 a;
time_t b;
...
fprintf(outFile,"%I64d,%I64d
",a,b); //I is capital i |
这是因为%llu在Windows下无法正常工作,%d无法处理64位整数。我建议改用PRIu64,你会发现它也可以移植到Linux上。
试试这个:
1 2 3 4 5 6 7 8 9 10 11
| #include <stdio.h>
#include <inttypes.h>
int main() {
unsigned long long int num = 285212672; //FYI: fits in 29 bits
int normalInt = 5;
/* NOTE: PRIu64 is a preprocessor macro and thus should go outside the quoted string. */
printf("My number is %d bytes wide and its value is %" PRIu64". A normal number is %d.
", sizeof(num), num, normalInt);
return 0;
} |
产量
1
| My number is 8 bytes wide and its value is 285212672. A normal number is 5. |
在Linux中它是%llu,在Windows中它是%I64u
虽然我发现它在Windows 2000中不起作用,但似乎有一个错误!
使用VS2005将其编译为x64:
%llu works well.
十六进制:
1
| printf("64bit: %llp", 0xffffffffffffffff); |
输出:
非标准的东西总是很奇怪:)
对于漫长的部分
在GNU下,它是L,ll或q
在Windows下,我认为它只是ll
除了几年前人们写的东西:
main.c:30:3: warning: unknown conversion type character 'l' in format [-Wformat=]
printf("%llu
", k);
然后您的mingw版本不会默认为c99。添加此编译器标志:-std=c99。
显然,自2008年以来,没有人提出多平台*解决方案超过十年,所以我会追加我的? Plz upvote。 (开玩笑。我不在乎。)
解决方案:lltoa()
如何使用:
1 2 3 4 5 6 7
| #include <stdlib.h> /* lltoa() */
// ...
char dummy[255];
printf("Over 4 bytes: %s
", lltoa(5555555555, dummy, 10));
printf("Another one: %s
", lltoa(15555555555, dummy, 10)); |
OP的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <stdio.h>
#include <stdlib.h> /* lltoa() */
int main() {
unsigned long long int num = 285212672; // fits in 29 bits
char dummy[255];
int normalInt = 5;
printf("My number is %d bytes wide and its value is %s."
"A normal number is %d.
",
sizeof(num), lltoa(num, dummy, 10), normalInt);
return 0;
} |
与%lld打印格式字符串不同,这个字符串适用于Windows下的32位GCC。
*)好吧,几乎是多平台。在MSVC中,您显然需要_ui64toa()而不是lltoa()。
好吧,一种方法是使用VS2008将其编译为x64
这可以按照您的预期运行:
1 2 3 4 5 6 7 8 9
| 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
",
sizeof(num),
num,
normalInt); |
对于32位代码,我们需要使用正确的__int64格式说明符%I64u。所以它变成了。
1 2 3 4 5 6 7
| 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编译器。