在某些平台上,int32_t(来自stdint.h)是long int,但在其他平台上,它可能是int.当我想使用时printf,我如何确定"%ld"或"%d"应该使用哪种格式?
或者,也许,我应该强制将其转换为如下所示:
int32_t m;
m = 3;
printf ("%ld\n", (long)m);
Run Code Online (Sandbox Code Playgroud)
但这种解决方案很乏味.有什么建议?
M.M*_*M.M 11
在C(自C99)中,inttypes.h包含扩展为固定宽度类型的格式说明符的宏.用于int32_t:
printf("%" PRId32 "\n", m);
Run Code Online (Sandbox Code Playgroud)
该宏可能会扩展到"d"或"ld".您可以使用常用的修饰符等,例如:
printf("%03" PRId32 "\n", m);
Run Code Online (Sandbox Code Playgroud)
在C++中(从C++ 11开始),可以使用相同的工具#include <inttypes.h>,或者#include <cinttypes>.
显然,一些C++实现需要用户编写#define __STDC_FORMAT_MACROS 1之前#include <inttypes.h>,即使C++标准规定是不需要的.
| 归档时间: |
|
| 查看次数: |
13988 次 |
| 最近记录: |