Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>fgetc()</code> returns <code>unsigned char</code> and EOF. EOF is always &lt; 0. If the system's <code>char</code> is <code>signed</code> or <code>unsigned</code>, it makes no difference.</p> <p>C11dr 7.21.7.1 2</p> <p>If the end-of-file indicator for the input stream pointed to by stream is not set and a next character is present, the fgetc function obtains that character as an <strong>unsigned char</strong> converted to an int and advances the associated file position indicator for the stream (if defined).</p> <p>The concern I have about is that is looks to be 2's compliment dependent and implying the range of <code>unsigned char</code> and <code>char</code> are both just as wide. Both of these assumptions are certainly <em>nearly</em> always true today. </p> <p><code>buf = (char) ((c &gt; CHAR_MAX) ? (c - (UCHAR_MAX + 1)) : c);</code></p> <p>[Edit per OP comment]<br> Let's assume <code>fgetc()</code> returns no more different characters than stuff-able in the range <code>CHAR_MIN</code> to <code>CHAR_MAX</code>, then <code>(c - (UCHAR_MAX + 1))</code> would be more portable is replaced with <code>(c - CHAR_MAX + CHAR_MIN)</code>. We do not know <code>(c - (UCHAR_MAX + 1))</code> is in range when <code>c is CHAR_MAX + 1</code>.</p> <p>A system <em>could</em> exist that has a <code>signed char</code> range of -127 to +127 and an <code>unsigned char</code> range 0 to 255. (5.2.4.2.1), but as <code>fgetc()</code> gets a <em>character</em>, it seems to have all be <code>unsigned char</code> <strong>or</strong> all ready limited itself to the smaller <code>signed char</code> range, before converting to <code>unsigned char</code> and return that value to the user. OTOH, if <code>fgetc()</code> returned 256 different characters, conversion to a narrow ranged <code>signed char</code> would not be portable regardless of formula.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload