55

I'm currently using a explicit cast to long and using %ld for printing pid_t, is there a specifier such as %z for size_t for pid_t?

If not what the best way of printing pid_t?

2
  • 1
    I am getting. %d hello.c:9:42: warning: format specifies type 'long' but the argument has type 'pid_t' (aka 'int') [-Wformat] when I try ld Jun 15, 2015 at 15:34
  • @KorayTugay Note the words above: "I'm currently using a explicit cast to long".
    – Jim Balter
    Apr 30, 2016 at 23:24

2 Answers 2

30

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.

6
  • Nice. If you would like to customize printf with your own spec for pid_t, check out this page: stackoverflow.com/questions/9260170/… Dec 12, 2013 at 1:59
  • Is the total number of processes related to how bits the os is? e.g can a os have more sizeof(int)? Dec 12, 2013 at 2:13
  • See 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.
    – Jim Balter
    Dec 12, 2013 at 4:47
  • 2
    Hmmm, 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. Dec 23, 2019 at 18:00
  • 3
    @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
    Dec 23, 2019 at 23:05
18

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.

pid_t pid = foo();

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

Or

// Since 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);

If sign-ness is not known for other system type, cast to the widest unsigned type or alternate opinion


1 The pid_t data type is a signed integer type which is capable of representing a process ID.

2
  • "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 to intmax_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.)
    – Jim Balter
    Dec 23, 2019 at 23:46
  • 1
    @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 to uintmax_t is more narrowly defined which does not lose info (hence my preference) than unsigned integer to intmax_t can. Choose your poison . Dec 24, 2019 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.