%" PRI*64"
int64_t id = 112233;
printf("id = %5" PRId64"",id);
这是一种跨平台的书写方式,主要是为了同时支持32位和64位操作系统
PRIu64 的定义在 inttypes.h 头文件里:
/* The ISO C99 standard specifies that these macros must only be
defined if explicitly requested. */
#if !defined __cplusplus || defined __STDC_FORMAT_MACROS
# if __WORDSIZE == 64
# define __PRI64_PREFIX "l"
# define __PRIPTR_PREFIX "l"
# else
# define __PRI64_PREFIX "ll"
# define __PRIPTR_PREFIX
# endif
/* Macros for printing format specifiers. */
/* Decimal notation. */
# define PRId8 "d"
# define PRId16 "d"
# define PRId32 "d"
# define PRId64 __PRI64_PREFIX "d"
/*
%d 有符号32位整数
%u 无符号32位整数
%lld 有符号64位整数
*/
32 位编译器,会把 "%"PRIu64 扩展为 “%lld”
64 位编译器,会把 "%"PRIu64 扩展为 “%ld”
不过这个是定义给C,如果C++或者想要用在androdi JNI platform
需要在头文件 或者 Android.mk 里加上一句:
LOCAL_CPPFLAGS := -D__STDC_FORMAT_MACROS
使用举例:
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
// g++ -g -o x x.cpp -D__STDC_FORMAT_MACROS -std=c++11
int main()
{
int64_t a = 32;
//printf("%"PRId64"\n", a);
printf("%" PRId64"\n", a); // 在PRId64前保留一个空格
// 如果不保留空格,则C++11编译时将报如下警告:
// invalid suffix on literal; C++11 requires a space between literal and identifier [-Wliteral-suffix]
// 将PRId64换成其它宏,情况相同
return 0;
}
%后面也可以加上数字来扩展长度
%5" PRId64" == %5ld 或者 %5lld