title: How to avoid integer promotion in C?
body: <p>It is not clear how to write portable code in C, using wide-character API. Consider this example:</p>
  
  <pre><code>#include &lt;locale.h&gt;
  #include &lt;wchar.h&gt;
  #include &lt;wctype.h&gt;
  int main(void)
  {
    setlocale(LC_CTYPE, "C.UTF-8");
    wchar_t wc = L'ÿ';
    if (iswlower(wc)) return 0;
    return 1;
  }
  </code></pre>
  
  <p>Compiling it with gcc-6.3.0 using -Wconversion option gives this warning:</p>
  
  <pre><code>test.c: In function 'main':
  test.c:9:16: warning: conversion to 'wint_t {aka unsigned int}' from 'wchar_t {aka int}' may change the sign of the result [-Wsign-conversion]
  if (iswlower(wc)) return 0;
               ^
  </code></pre>
  
  <p>To get rid of this warning, we cast to <code>(wint_t)</code>, like <code>iswlower((wint_t)wc)</code>, but this is unportable.
  The following example demonstrates why it is unportable.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  /* this is our hypothetical implementation */
  typedef signed int wint_t;
  typedef signed short wchar_t;
  #define WEOF ((wint_t)0xffffffff)
  
  void f(wint_t wc)
  {
      if (wc==WEOF)
        printf("BUG. Valid character recognized as WEOF. This is due to integer promotion. How to avoid it?\n");
  }
  int main(void)
  {
      wchar_t wc = (wchar_t)0xffff;
      f((wint_t)wc);
      return 0;
  }
  </code></pre>
  
  <p>My question is: how to make this example portable, and at the same time avoid the gcc warning.</p>