What is the correct printf specifier for printing pid_t
Solution 1
There's no such specifier. I think what you're doing (casting the pid_t
to long
and printing it with "%ld"
) 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.
Solution 2
With integer types lacking a matching format specifier as in the case of pid_t
, yet with known sign-ness1, cast to widest matching signed type and print.
If sign-ness is not known for other system type, cast to the widest unsigned type or alternate opinion
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);
1
The pid_t
data type is a signed integer type which is capable of representing a process ID.
Bilal Syed Hussain
Updated on August 13, 2020Comments
-
Bilal Syed Hussain over 3 years
I'm currently using a explicit cast to long and using
%ld
for printingpid_t
, is there a specifier such as%z
forsize_t
forpid_t
?If not what the best way of printing
pid_t
? -
Mad Physicist over 10 yearsNice. If you would like to customize
printf
with your own spec forpid_t
, check out this page: stackoverflow.com/questions/9260170/… -
Bilal Syed Hussain over 10 yearsIs the total number of processes related to how bits the os is? e.g can a os have more sizeof(int)?
-
Jim Balter over 10 yearsSee stackoverflow.com/questions/1922761/… ... pid_t is usually 32 bits, regardless of the OS bit size. A system could have larger ones, but I wouldn't expect it. In any case, long will be plenty for safety.
-
chux - Reinstate Monica over 4 yearsHmmm, if pid_t is usually 32 bits, regardless of the OS bit size. is true then on implementations where
long
is 64-bit, printing with a"%ld"
will certainly cause issues. -
Jim Balter over 4 years@chux-ReinstateMonica Please read the question: "I'm currently using a explicit cast to long and using %ld" -- that's what I said "is fine". Of course there is a problem with using %ld without guaranteeing that a long is being passed.
-
Jim Balter over 4 years"If sign-ness is not known, cast to the widest unsigned type" -- I would not advise this. Casting to
uintmax_t
and printing with%ju
will print all negative values incorrectly, whereas casting tointmax_t
and printing with%jd
will only print values >INTMAX_MAX
incorrectly. (Then again, situations in which one really doesn't know whether a type includes negative values are quite rare.) -
chux - Reinstate Monica over 4 years@JimBalter Fair point. With sign-ness unknown there is the trade-off of wrongly printing negative value as large values or large values as negative. I'd expect when sign-ness unknown, it is usually more natural to use
intmax_t
as small values are more common, yet conversion signed integer touintmax_t
is more narrowly defined which does not lose info (hence my preference) than unsigned integer tointmax_t
can. Choose your poison . -
alx about 4 years@MadPhysicist you could also write a macro similar to
PRIi32
:#define PRIpid "i"
.