GCC does not generate compilation warning

The following code compiles and runs, but I expect a warning when compiling:

#include <stdio.h> #include <stdlib.h> int main(void){ int x = 10; printf("%p\n",&x); return EXIT_SUCCESS; } 

GCC, from an online compiler with a command line argument

 -Wall -std=gnu99 -O2 -o a.out source_file.c -pedantic -Wextra 

gives the following warning at compilation

 source_file.c: In function 'main': source_file.c:7:3: warning: format '%p' expects argument of type 'void *', but argument 2 has type 'int *' [-Wformat=] printf("%p\n",&x); 

because I did not add the cast (void*) before &x , since %p expects an argument of type void* . But when I compile with

 gcc SO.c -o so -Wall -Wextra -pedantic -std=c11 

or

 gcc SO.c -o so -Wall -Wextra -pedantic -std=c99 

or

 gcc SO.c -o so -Wall -Wextra -pedantic -std=c89 

GCC (on my PC) does not issue a warning, while compiling (again on my PC) using

 gcc SO.c -o so -Wall -Wextra -pedantic -std=gnu11 

or

 gcc SO.c -o so -Wall -Wextra -pedantic -std=gnu99 

or

 gcc SO.c -o so -Wall -Wextra -pedantic -std=gnu89 

or

 gcc SO.c -o so -Wall -Wextra -pedantic 

I get the warning mentioned above. Why is that? My version of GCC is 4.8.1 and I am using Windows. I am compiling from the console, i.e. cmd .

+8
c gcc gnu
source share
2 answers

Why is that?

Firstly, I can also reproduce this inconsistent state with mingw32 gcc 4.8.1 on my machine.

While diagnostics are not required (without limitation) by the C standard, there is no reason why gcc issues diagnostics using -std=gnu11 rather than using -std=c11 .

Also, with gcc 4.8.2 on Linux on my machine, diagnostics are displayed with both -std=c11 and -std=gnu11 .

Thus, it looks like an error in gcc (either in gcc 4.8.1, or in mingw32 gcc 4.8.1).

+2
source share

The documentation for GNU libc and C99 libc (page 315 pdf ) explicitly indicates that the argument for converting %p must be of type void * . "

The fact that a warning is not issued should be a feature of what your compiler thinks of Cxx standards. The gcc 4.8.2 promotion on Ubuntu 14.04 gives a warning for all six standards mentioned in your post.

0
source share

All Articles