PRId64正确使用来保证跨平台

简介: int64_t用来表示64位整数,在32位系统中是long long int,在64位系统中是long int,所以打印int64_t的格式化方法是: [cpp] view plaincopy printf("%ld", value...

int64_t用来表示64位整数,在32位系统中是long long int,在64位系统中是long int,所以打印int64_t的格式化方法是:

[cpp]  view plain copy
  1. printf("%ld", value); // 64bit OS  
  2. printf("%lld", value); // 32bit OS  
当然有跨平台的方法:

[cpp]  view plain copy
  1. #include <inttypes.h>  
  2. printf("%" PRId64 "\n", value);  
  3. // 相当于64位的:  
  4. printf("%" "ld" "\n", value);  
  5. // 或32位的:  
  6. printf("%" "lld" "\n", value);  
其中,printf("abc" "def" “ghi")这样写多个字符串是没有问题的。

但是,死活都编译不过,错误是:error: expected ‘)’ before ‘PRId64’

找了一下这个宏的定义,/usr/include/inttypes.h:

[cpp]  view plain copy
  1. /* The ISO C99 standard specifies that these macros must only be 
  2.    defined if explicitly requested.  */  
  3. #if !defined __cplusplus || defined __STDC_FORMAT_MACROS  
  4.   
  5. # if __WORDSIZE == 64  
  6. #  define __PRI64_PREFIX    "l"  
  7. #  define __PRIPTR_PREFIX   "l"  
  8. # else  
  9. #  define __PRI64_PREFIX    "ll"  
  10. #  define __PRIPTR_PREFIX  
  11. # endif  
  12.   
  13. /* Macros for printing format specifiers.  */  
  14.   
  15. /* Decimal notation.  */  
  16. # define PRId8      "d"  
  17. # define PRId16     "d"  
  18. # define PRId32     "d"  
  19. # define PRId64     __PRI64_PREFIX "d"  
原来这个是定义给c用的,C++要用它,就要定义一个__STDC_FORMAT_MACROS宏显示打开它。

[cpp]  view plain copy
  1. /* test_int64.cpp 
  2. g++ -D__STDC_FORMAT_MACROS -o test_int64 -g -O0 test_int64.cpp 
  3. */  
  4. #include <stdio.h>  
  5. #include <inttypes.h>  
  6.   
  7. int main(int argc, char** argv){  
  8.     int64_t value = 0xFFFFFFFFFFFF;  
  9.     printf("int64_t=%"PRId64", sizeof(int64_t)=%d\n", value, sizeof(int64_t));  
  10. }  

编译并执行:

g++ -D__STDC_FORMAT_MACROS -o test_int64 -g -O0 test_int64.cpp

./test_int64

int64_t=281474976710655, sizeof(int64_t)=8

对于C++新标准-std=c++0x,还可以使用更好的方式:

[cpp]  view plain copy
  1. /* test_int64_1.cpp  
  2. g++ -o test_int64_1 -g -O0 test_int64_1.cpp 
  3. */  
  4. #include <stdio.h>  
  5. #include <cinttypes>  
  6. using namespace std;  
  7.   
  8. int main(int argc, char** argv){  
  9.     int64_t value = 0xFFFFFFFFFFFF;  
  10.     printf("int64_t=%"PRId64", sizeof(int64_t)=%d\n", value, sizeof(int64_t));  
  11. }  
不用定义那个宏了,编译和执行:

g++ -o test_int64_1 -g -O0 test_int64_1.cpp -std=c++0x

./test_int64_1

int64_t=281474976710655, sizeof(int64_t)=8

当然得指定一个新的参数:-std=c++0x,否则会报错“#error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.”

若能使用较新的g++编译,可以使用后者,否则可以用前者直接定义宏。

目录
相关文章
|
自然语言处理 安全 C++
【C++ 格式化输出 】C++20 现代C++格式化:拥抱std--format简化你的代码
【C++ 格式化输出 】C++20 现代C++格式化:拥抱std--format简化你的代码
9413 4
|
网络协议 IDE Linux
mongoose使用详细 -- 如何通过mongoose搭建服务器
mongoose使用详细 -- 如何通过mongoose搭建服务器
2362 0
|
Python Windows
PyCharm证书过期:Your license has expired
pycharm激活码,由于那台服务器不维护了,不好使了,你可以关注宏哥的公众号发送:激活码,Ctrl+A,Ctrl+C,然后Ctrl+V,就可以了。
5326 0
PyCharm证书过期:Your license has expired
|
Linux KVM 虚拟化
windwos上通过qemu直接开启img、qcow2等格式磁盘镜像(无需转vmdk)
QEMU 是一款开源虚拟化软件,支持多种硬件平台和虚拟化技术,如 KVM 加速。它可以在 Windows、Linux 和 macOS 上运行。本文介绍了 QEMU 的下载、安装、配置虚拟网卡、启动虚拟机、网络通信及快照管理等步骤。通过 QEMU,用户可以轻松创建和管理虚拟机,实现高效的开发和测试环境。
5549 0
windwos上通过qemu直接开启img、qcow2等格式磁盘镜像(无需转vmdk)
|
Ubuntu 安全 网络协议
|
JSON 安全 API
⚡什么是 OpenAPI,优势、劣势及示例
OpenAPI 是一个用于描述RESTful API的标准,它提供了一个接口,使得人和机器无需源代码或文档就能理解服务。它定义了API的结构,与语言无关,适用于REST API。OpenAPI始于Swagger项目,后来成为OpenAPI倡议的一部分,由Linux基金会管理,得到了众多公司的支持。OpenAPI流行的原因包括其语言无关性、可读性、社区支持和工具生态系统。它使用JSON格式,支持各种数据类型,并具有严格定义的结构。虽然有其他如RAML和API Blueprint的竞争格式,但OpenAPI的广泛采用使其成为行业标准。
|
Prometheus 算法 Cloud Native
熔断原理分析与源码解读
熔断原理分析与源码解读
|
SQL 关系型数据库 MySQL
MySQL Online DDL原理解读
MySQL Online DDL原理解读
368 3
|
编译器 C++
fmt文本格式库的源码下载编译(Win10+VS2022)
fmt文本格式库的源码下载编译(Win10+VS2022)
767 0