What is the correct printf specifier for printing pid_t

强颜欢笑 提交于 2019-11-30 16:47:58

There's no such specifier. I think what you're doing is fine… you could use an even wider int type, but there's no implementation where pid_t is bigger than long and probably never will be.

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);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!