[英]How to print a int64_t type in C
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 变量?
对于int64_t
类型:
#include <inttypes.h>
int64_t t;
printf("%" PRId64 "\n", t);
对于uint64_t
类型:
#include <inttypes.h>
uint64_t t;
printf("%" PRIu64 "\n", t);
您还可以使用PRIx64
以十六进制打印。
cppreference.com 有所有类型的可用宏的完整列表,包括intptr_t
( PRIxPTR
)。 scanf 有单独的宏,比如SCNd64
。
PRIu16 的典型定义是"hu"
,因此隐式字符串常量连接发生在编译时。
为了让您的代码完全可移植,您必须使用PRId32
等来打印int32_t
,并使用"%d"
或类似的方式来打印int
。
C99方式是
#include <inttypes.h>
int64_t my_int = 999999999999999999;
printf("%" PRId64 "\n", my_int);
或者你可以投!
printf("%ld", (long)my_int);
printf("%lld", (long long)my_int); /* C89 didn't define `long long` */
printf("%f", (double)my_int);
如果您坚持使用 C89 实现(特别是 Visual Studio),您或许可以使用开源<inttypes.h>
(和<stdint.h>
): http : //code.google.com/p/msinttypes/
在 C99 中, %j
长度修饰符也可以与 printf 系列函数一起使用来打印int64_t
和uint64_t
类型的值:
#include <stdio.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
int64_t a = 1LL << 63;
uint64_t b = 1ULL << 63;
printf("a=%jd (0x%jx)\n", a, a);
printf("b=%ju (0x%jx)\n", b, b);
return 0;
}
使用gcc -Wall -pedantic -std=c99
编译此代码不会产生警告,并且程序会打印预期的输出:
a=-9223372036854775808 (0x8000000000000000)
b=9223372036854775808 (0x8000000000000000)
这是根据我的 Linux 系统上的printf(3)
(手册页特别说明j
用于表示转换为intmax_t
或uintmax_t
;在我的 stdint.h 中, int64_t
和intmax_t
类型定义完全相同方式,对于uint64_t
也类似)。 我不确定这是否可以完美移植到其他系统。
来自嵌入式世界,在那里甚至 uclibc 并不总是可用,并且代码如下
uint64_t myval = 0xdeadfacedeadbeef; printf("%llx", myval);
打印你的废话或根本不工作 - 我总是使用一个小助手,它允许我正确转储 uint64_t 十六进制:
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
char* ullx(uint64_t val)
{
static char buf[34] = { [0 ... 33] = 0 };
char* out = &buf[33];
uint64_t hval = val;
unsigned int hbase = 16;
do {
*out = "0123456789abcdef"[hval % hbase];
--out;
hval /= hbase;
} while(hval);
*out-- = 'x', *out = '0';
return out;
}
在windows环境下,使用
%I64d
在 Linux 中,使用
%lld
//VC6.0(386及更好)
__int64 my_qw_var = 0x1234567890abcdef;
__int32 v_dw_h;
__int32 v_dw_l;
__asm
{
mov eax,[dword ptr my_qw_var + 4] //dwh
mov [dword ptr v_dw_h],eax
mov eax,[dword ptr my_qw_var] //dwl
mov [dword ptr v_dw_l],eax
}
//Oops 0.8 format
printf("val = 0x%0.8x%0.8x\n", (__int32)v_dw_h, (__int32)v_dw_l);
问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.