title: How to portably convert a string into an uncommon integer type?
body: <p>Some background: If I wanted to use for, for instance, <code>scanf()</code> to convert a string into a standard integer type, like <code>uint16_t</code>, I’d use <code>SCNu16</code> from <code>&lt;inttypes.h&gt;</code>, like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;inttypes.h&gt;
  uint16_t x;
  char *xs = "17";
  sscanf(xs, "%" SCNu16, &amp;x);
  </code></pre>
  
  <p>But a more uncommon integer type like <code>pid_t</code> does not have any such thing; only the normal integer types are supported by <code>&lt;inttypes.h&gt;</code>.  To convert the other way, to portably <code>printf()</code> a <code>pid_t</code>, I can cast it to <code>intmax_t</code> and use <code>PRIdMAX</code>, like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;inttypes.h&gt;
  #include &lt;sys/types.h&gt;
  pid_t x = 17;
  printf("%" PRIdMAX, (intmax_t)x);
  </code></pre>
  
  <p>However, there does not seem to be a way to portably <code>scanf()</code> into a <code>pid_t</code>.  So this is my question: How to do this portably?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  pid_t x;
  char *xs = 17;
  sscanf(xs, "%u", &amp;x);  /* Not portable! pid_t might not be int! /*
  </code></pre>
  
  <p>I thought of <code>scanf()</code>ing to an <code>intmax_t</code> and then checking that the value is within <code>pid_t</code>’s limits before casting to <code>pid_t</code>, but there does not seem to be a way to get the maximum or minimum values for <code>pid_t</code>.</p>