8

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 command line argument

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

gives out the following warning when compiling

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've not added a (void*) cast before &x as %p expects an argument of type void* .But when I compile using

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(in my PC) does not give out a warning whereas compiling(again in 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 it like that? My GCC version is 4.8.1 and I'm using Windows. I compile from the console,i.e, cmd.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83

2 Answers2

2

Why is it like that?

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

While the diagnostic is not required (no constraint violation) by the C Standard, there is no reason gcc issues the diagnostic with -std=gnu11 and not with -std=c11.

Moreover with gcc 4.8.2 on Linux on my machine, the diagnostic appears with both -std=c11 and -std=gnu11.

So it looks like a bug in gcc (either in gcc 4.8.1 or in mingw32 gcc 4.8.1).

ouah
  • 142,963
  • 15
  • 272
  • 331
  • I'll assume mingw lacks an `__attribute__((format(printf,3,4)))` in the stdio header files of mingw on the printf() declaration - without that, printf is just a normal varadic function, and no format checking can be provided by -Wformat – nos Jan 03 '15 at 14:00
  • 1
    @nos could be but gcc documentation of format attribute says that *The compiler always (unless -ffreestanding or -fno-builtin is used) checks formats for the standard library functions printf, [...] whenever such warnings are requested (using -Wformat), so there is no need to modify the header file stdio.h* so it seems even without the attribute gcc should warn. – ouah Jan 03 '15 at 14:10
  • @ouah , Is this question related to [this one](http://stackoverflow.com/questions/27363795/mingw-doesnt-produce-warnings)? – Spikatrix Jan 03 '15 at 14:13
  • @CoolGuy nice catch, yes it seems so and I think your question should be marked as a duplicate. – ouah Jan 03 '15 at 14:16
0

Documentation for both GNU libc and C99 libc (page 315 of the pdf) explicitly says that the argument for %p conversion "must be of type void *".

The fact that the warning is not issued should be a particularity of what your compiler thinks about Cxx standards. A stock gcc 4.8.2 on Ubuntu 14.04 issues the warning for all the six standards mentioned in your post.

Konstantin Shemyak
  • 2,369
  • 5
  • 21
  • 41