我目前正在使用明确的强制转换来使用%ld
进行打印pid_t
,%z
的{{1}}是size_t
的说明符吗pid_t
}?
如果不是最好的打印方式pid_t
?
答案 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);