c - 如何打印大小未知的类型,如 ino_t?

标签 c printf portability

我经常遇到这样的情况,我想用 printf 打印实现定义大小的整数类型的值(比如 ino_ttime_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, or X conversion specifier applies to an intmax_t or uintmax_t argument; or that a following n conversion specifier applies to a pointer to an intmax_t argument.

还有zt 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,最宽的整数类型是 longunsigned long , 你可以转换成那些并使用 "%ld"和/或 "%lu" .您可以使用 __STDC_VERSION__ 理论上测试 C99 合规性预定义的宏——虽然一些 C99 之前的编译器可能仍然支持比 long 宽的类型和 unsigned long作为扩展。

关于c - 如何打印大小未知的类型,如 ino_t?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24844970/

相关文章:

c - 使用指针将字符串从源附加到目标

c - 打印 C 中指针的值

c - printf 在 scanf 之后不打印?

c - scanf(或)printf 中的错误格式说明符

survey - 您的 USB 驱动器上带有哪些开发工具?

c - 确定井字游戏中的最佳 Action

在 Rust 中创建 C 函数指针的接口(interface)

gcc -pthread 和 -pthreads 之间的区别?

c++ - 如何在 C 或 C++ 中获得与 Java 中的 toLowerCase 或 Python 中的 string.lower() 相同的结果?

在 C 中创建没有依赖项的 Gotoxy() 函数