C++中正确使用PRId64
来源:互联网 发布:淘宝交易额实时数据 编辑:程序博客网 时间:2024/05/05 04:30
nt64_t用来表示64位整数,在32位系统中是long long int,在64位系统中是long int,所以打印int64_t的格式化方法是:
[cpp] view plaincopy
- printf("%ld", value); // 64bit OS
- printf("%lld", value); // 32bit OS
[cpp] view plaincopy
- #include <inttypes.h>
- printf("%" PRId64 "\n", value);
- // 相当于64位的:
- printf("%" "ld" "\n", value);
- // 或32位的:
- printf("%" "lld" "\n", value);
但是,死活都编译不过,错误是:error: expected ‘)’ before ‘PRId64’
找了一下这个宏的定义,/usr/include/inttypes.h:
[cpp] view plaincopy
- /* 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"
[cpp] view plaincopy
- /* test_int64.cpp
- g++ -D__STDC_FORMAT_MACROS -o test_int64 -g -O0 test_int64.cpp
- */
- #include <stdio.h>
- #include <inttypes.h>
- int main(int argc, char** argv){
- int64_t value = 0xFFFFFFFFFFFF;
- printf("int64_t=%"PRId64", sizeof(int64_t)=%d\n", value, sizeof(int64_t));
- }
编译并执行:
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 plaincopy
- /* test_int64_1.cpp
- g++ -o test_int64_1 -g -O0 test_int64_1.cpp
- */
- #include <stdio.h>
- #include <cinttypes>
- using namespace std;
- int main(int argc, char** argv){
- int64_t value = 0xFFFFFFFFFFFF;
- printf("int64_t=%"PRId64", sizeof(int64_t)=%d\n", value, sizeof(int64_t));
- }
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++编译,可以使用后者,否则可以用前者直接定义宏。
0 0
- C++中正确使用PRId64
- C++中正确使用PRId64
- C++中正确使用PRId64
- C++中正确使用PRId64
- C++中正确使用PRId64
- C++中正确使用PRId64
- C++中正确使用PRId64
- PRId64正确使用来保证跨平台
- 使用PRId64
- PRId64的正确用法
- C语言中正确使用const
- C/C++中const的正确使用
- C++-正确使用memset
- 如何在C#Winform程序中正确使用登录窗体
- C语言中正确使用输入函数scanf
- C语言中extern关键字的正确使用
- C++/*注释*/正确的使用
- C语言的正确使用
- 线性表
- LCD12864硬件连接
- nova-compute[5410]: OSError: [Errno 13] Permission denied: '镜像路径'
- 为什么我的app老是不流畅,到底哪里出了问题?
- GUI的学习笔记 version1.0
- C++中正确使用PRId64
- 堆排序
- [LeetCode] Rotate List
- BZOJ 4145 AMPPZ2014 The Prices 状压DP
- 怎么像safari一样滑动的时候隐藏navigationbar?
- AIX下安装GNU make
- 【Java线程】锁机制:synchronized、Lock、Condition
- 我是谁
- oracle故障代码