我经常遇到这样的情况,我想用 printf
打印实现定义大小的整数类型的值(比如 ino_t
或 time_t
).现在,我为此使用了这样的模式:
#include <inttypes.h>
ino_t ino; /* variable of unknown size */
printf("%" PRIuMAX, (uintmax_t)ino);
这种方法目前有效,但它有几个缺点:
- 我必须知道我尝试打印的类型是签名的还是未签名的。
- 我必须使用可能会扩大我的代码的类型转换。
有没有更好的策略?
最佳答案
#include <inttypes.h>
ino_t ino; /* variable of unknown size */
/* ... */
printf("%" PRIuMAX, (uintmax_t)ino);
这当然可行(有一些附带条件;见下文),但我会使用:
printf("%ju", (uintmax_t)ino);
j
长度修饰符
Specifies that a following
d
,i
,o
,u
,x
, orX
conversion specifier applies to anintmax_t
oruintmax_t
argument; or that a followingn
conversion specifier applies to a pointer to anintmax_t
argument.
还有z
和 t
size_t
的修饰符和 ptrdiff_t
(及其相应的有符号/无符号类型)。
就我个人而言,我发现格式字符串宏定义在 <inttypes.h>
中丑陋且难以记住,这就是为什么我更喜欢"%ju"
或 "%jd"
.
如您所述,了解类型(在本例中为 ino_t
)是有符号的还是无符号的会很有帮助。如果您碰巧不知道这一点,则可以弄清楚:
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#define IS_SIGNED(type) ((type)-1 < (type)0)
#define DECIMAL_FORMAT(type) (IS_SIGNED(type) ? "%jd" : "%ju")
#define CONVERT_TO_MAX(type, value) \
(IS_SIGNED(type) ? (intmax_t)(value) : (uintmax_t)(value))
#define PRINT_VALUE(type, value) \
(printf(DECIMAL_FORMAT(type), CONVERT_TO_MAX(type, (value))))
int main(void) {
ino_t ino = 42;
PRINT_VALUE(ino_t, ino);
putchar('\n');
}
虽然这可能有点矫枉过正。如果您确定类型小于 64 位,则可以将该值转换为 intmax_t
,并且该值将被保留。或者你可以使用 uintmax_t
并获得所有值的明确定义的结果,尽管打印 -1
作为18446744073709551615
(264-1) 可能有点令人困惑。
只有当您的 C 实现支持 <stdint.h>
时,所有这些才有效和 j
printf
的长度修饰符-- 即,如果它支持 C99。并非所有编译器都这样做(咳咳Microsoft咳咳)。对于 C90,最宽的整数类型是 long
和 unsigned long
, 你可以转换成那些并使用 "%ld"
和/或 "%lu"
.您可以使用 __STDC_VERSION__
理论上测试 C99 合规性预定义的宏——虽然一些 C99 之前的编译器可能仍然支持比 long
宽的类型和 unsigned long
作为扩展。
关于c - 如何打印大小未知的类型,如 ino_t?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24844970/