打印pid_t的printf说明符是什么

时间:2013-12-12 01:54:06

标签: c io printf pid

我目前正在使用明确的强制转换来使用%ld进行打印pid_t%z的{​​{1}}是size_t的说明符吗pid_t }?

如果不是最好的打印方式pid_t

2 个答案:

答案 0 :(得分:15)

没有这样的说明符。我认为你所做的很好......你可以使用更宽的int类型,但是没有实现pid_t大于long并且可能永远不会。

答案 1 :(得分:8)

With integer types lacking a matching format specifier as in the case of pid_t, yet with known sign-ness, cast to widest matching signed type and print. If sign-ness is not known, cast to the widest unsigned type.

pid_t pid = foo();

// C99
#include <stdint.h>
printf("pid = %jd\n", (intmax_t) pid);

Or

// C99
#include <stdint.h>
#include <inttypes.h>
printf("pid = %" PRIdMAX "\n", (intmax_t) pid);

Or

// pre-C99
pid_t pid = foo();
printf("pid = %ld\n", (long) pid);