如何在 C 中便携式打印 int64_t 类型
C99 标准具有字节大小的整数类型,如 int64_t。我当前使用 Windows 的 %I64d
格式(或无符号 %I64u
),例如:
#include <stdio.h>
#include <stdint.h>
int64_t my_int = 999999999999999999;
printf("This is my_int: %I64d\n", my_int);
我收到此编译器警告:
warning: format ‘%I64d’ expects type ‘int’, but argument 2 has type ‘int64_t’
我尝试过:
printf("This is my_int: %lld\n", my_int); // long long decimal
但我收到相同的警告。我正在使用这个编译器:
~/dev/c$ cc -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5664~89/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)
我应该使用哪种格式来打印 my_int 变量而不发出警告?
C99 standard has integer types with bytes size like int64_t. I am using Windows's %I64d
format currently (or unsigned %I64u
), like:
#include <stdio.h>
#include <stdint.h>
int64_t my_int = 999999999999999999;
printf("This is my_int: %I64d\n", my_int);
and I get this compiler warning:
warning: format ‘%I64d’ expects type ‘int’, but argument 2 has type ‘int64_t’
I tried with:
printf("This is my_int: %lld\n", my_int); // long long decimal
But I get the same warning. I am using this compiler:
~/dev/c$ cc -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5664~89/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)
Which format should I use to print my_int variable without having a warning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对于
int64_t
类型:对于
uint64_t
类型:您还可以使用
PRIx64
以十六进制打印。cppreference.com 提供了所有类型的可用宏的完整列表,包括 <代码>intptr_t(
PRIxPTR
)。 scanf 有单独的宏,例如SCNd64
。PRIu16 的典型定义是
"hu"
,因此隐式字符串常量连接发生在编译时。为了使您的代码完全可移植,您必须使用
PRId32
等来打印int32_t
,并使用"%d"
或类似的方式来打印 <代码>int。For
int64_t
type:for
uint64_t
type:you can also use
PRIx64
to print in hexadecimal.cppreference.com has a full listing of available macros for all types including
intptr_t
(PRIxPTR
). There are separate macros for scanf, likeSCNd64
.A typical definition of PRIu16 would be
"hu"
, so implicit string-constant concatenation happens at compile time.For your code to be fully portable, you must use
PRId32
and so on for printingint32_t
, and"%d"
or similar for printingint
.C99 的方式是
或者你可以铸造!
如果您坚持使用 C89 实现(尤其是 Visual Studio),您也许可以使用开源
(和
) :http://code.google.com/p/msinttypes/The C99 way is
Or you could cast!
If you're stuck with a C89 implementation (notably Visual Studio) you can perhaps use an open source
<inttypes.h>
(and<stdint.h>
): http://code.google.com/p/msinttypes/在 C99 中,
%j
长度修饰符还可以与 printf 系列函数一起使用,以打印int64_t
和uint64_t
类型的值:编译此代码使用
gcc -Wall -pedantic -std=c99
不会产生警告,并且程序会打印预期的输出:这是根据我的 Linux 系统上的
printf(3)
(手册页明确指出,j
用于指示到intmax_t
或uintmax_t
的转换;在我的 stdint.h 中,两者都int64_t
和intmax_t
的类型定义方式完全相同,uint64_t
也类似)。我不确定这是否可以完美移植到其他系统。With C99 the
%j
length modifier can also be used with the printf family of functions to print values of typeint64_t
anduint64_t
:Compiling this code with
gcc -Wall -pedantic -std=c99
produces no warnings, and the program prints the expected output:This is according to
printf(3)
on my Linux system (the man page specifically says thatj
is used to indicate a conversion to anintmax_t
oruintmax_t
; in my stdint.h, bothint64_t
andintmax_t
are typedef'd in exactly the same way, and similarly foruint64_t
). I'm not sure if this is perfectly portable to other systems.来自嵌入式世界,甚至 uclibc 也并不总是可用,并且代码类似于
uint64_t myval = 0xdeadfacedeadbeef;
printf("%llx", myval);
正在打印你的垃圾或者根本不起作用——我总是使用一个小助手,它允许我正确转储 uint64_t 十六进制:
Coming from the embedded world, where even uclibc is not always available, and code like
uint64_t myval = 0xdeadfacedeadbeef;
printf("%llx", myval);
is printing you crap or not working at all -- i always use a tiny helper, that allows me to dump properly uint64_t hex:
在windows环境下使用,
在Linux下使用
In windows environment, use
in Linux, use
当我寻找一种以十六进制显示 64 位数字的方法时,偶然发现了这个问题:
发现您可以使用:
0x%016llx
- 至少适用于我的编译器(aarch64 GCC 7.3 .0
)Stumbled upon this question when I was looking for a way to display 64 bit number in hex:
Found out that you can use:
0x%016llx
- at least works with my compiler (aarch64 GCC 7.3.0
)//VC6.0(386 及更好)
问候。
//VC6.0 (386 & better)
Regards.