title: Should C compilers warn on 'char c = NULL'?
body: <p>Take this brief C file, <strong>nulltest.c</strong>, which prints "Hey":</p>
  
  <pre><code>#include &lt;stddef.h&gt;
  #include &lt;stdio.h&gt;
  
  int main() {
    char c = NULL;
    c = 'e';
    printf("H%cy\n", c);
    return 0;
  }
  </code></pre>
  
  <p>My understanding is that in C, <code>NULL</code> <a href="http://c-faq.com/null/macro.html" rel="noreferrer">should expand to a null pointer constant</a>, which would make <code>char c = NULL</code> an implicit cast of pointer to integer. However, when compiling this in gcc 4.8:</p>
  
  <pre><code>$ gcc --version
  gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
  $ gcc -Wall -Wconversion nulltest.c
  $
  </code></pre>
  
  <p>I get no warnings.</p>
  
  <p>On the other hand, both a prior version of gcc and clang warn on the same code:</p>
  
  <pre><code>$ gcc --version
  gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
  $ gcc nulltest.c
  nulltest.c: In function ‘main’:
  nulltest.c:5: warning: initialization makes integer from pointer without a cast
  </code></pre>
  
  <p>On Mac OS X 10.9:</p>
  
  <pre><code>$ clang --version
  Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn)
  $ clang nulltest.c
  nulltest.c:5:8: warning: incompatible pointer to integer conversion initializing
        'char' with an expression of type 'void *' [-Wint-conversion]
    char c = NULL;
         ^   ~~~~
  </code></pre>
  
  <p>For both gcc 4.4 and 4.8, I believe the <a href="http://gcc.gnu.org/viewcvs/gcc/trunk/gcc/ginclude/stddef.h?revision=206289&amp;view=markup" rel="noreferrer">relevant line in the relevant copy of stddef.h</a> reads <code>#define NULL ((void *)0)</code>.</p>
  
  <p>Why does one version of gcc warn and not the other?</p>