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?

Bilal Syed Hussain
  • 8,664
  • 11
  • 38
  • 44
  • 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 – Koray Tugay Jun 15 '15 at 15:34
  • @KorayTugay Note the words above: "I'm currently using a explicit cast to long". – Jim Balter Apr 30 '16 at 23:24

2 Answers2

29

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.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • Nice. If you would like to customize `printf` with your own spec for `pid_t`, check out this page: http://stackoverflow.com/questions/9260170/is-this-possible-to-customize-printf – Mad Physicist Dec 12 '13 at 01:59
  • Is the total number of processes related to how bits the os is? e.g can a os have more sizeof(int)? – Bilal Syed Hussain Dec 12 '13 at 02:13
  • See http://stackoverflow.com/questions/1922761/size-of-pid-t-uid-t-gid-t-on-linux ... 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 '13 at 04:47
  • 2
    Hmmm, if [pid_t is usually 32 bits, regardless of the OS bit size.](https://stackoverflow.com/questions/20533606/what-is-the-correct-printf-specifier-for-printing-pid-t/44504623#comment30706088_20533635) is true then on implementations where `long` is 64-bit, printing with a `"%ld"` will certainly cause issues. – chux - Reinstate Monica Dec 23 '19 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 '19 at 23:05
  • @MadPhysicist you could also write a macro similar to `PRIi32`: `#define PRIpid "i"`. – alx - recommends codidact Feb 27 '20 at 12:10
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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • "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 '19 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](https://www.merriam-webster.com/dictionary/pick%2Fchoose%20your%20poison) . – chux - Reinstate Monica Dec 24 '19 at 00:12