Take this brief C file, nulltest.c, which prints "Hey":
#include <stddef.h>
#include <stdio.h>
int main() {
char c = NULL;
c = 'e';
printf("H%cy\n", c);
return 0;
}
My understanding is that in C, NULL
should expand to a null pointer constant, which would make char c = NULL
an implicit cast of pointer to integer. However, when compiling this in gcc 4.8:
$ gcc --version
gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
$ gcc -Wall -Wconversion nulltest.c
$
I get no warnings.
On the other hand, both a prior version of gcc and clang warn on the same 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
On Mac OS X 10.9:
$ 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;
^ ~~~~
For both gcc 4.4 and 4.8, I believe the relevant line in the relevant copy of stddef.h reads #define NULL ((void *)0)
.
Why does one version of gcc warn and not the other?