5
#include<stdio.h>
void main()
{
   int i = 5;
   printf("%p",i);
}

I tried to compile this program on Linux using GCC compiler which on compilation of the program issues a warning saying

%p expects a void* pointer 

and when run gives an output of 46600x3.

But when I compile it online using the site codingground.tutorialspoint.com I get an output equals 0x5 i.e. a hexadecimal output, can anyone please explain the reason?

Maroun
  • 94,125
  • 30
  • 188
  • 241

2 Answers2

6

%p expects the address of something(or a pointer). You provide the value of the variable i instead of the address. Use

printf("%p",(void*)&i);

instead of

printf("%p",i);

and GCC will compile the program without warnings and it will print the address where i is stored when you run it. The ampersand(&) is the address-of operator and it gives the address of the variable. The cast is also neccessary as the format specifier %p expects an argument of type void* whereas &i is of type int*.


If you want the value of the variable to be printed,use
printf("%d",i);

Using the wrong format specifier will lead to UB(Undefined Behavior)

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 1
    `p` requires a `(void *)` argument. You have to use `printf("%p", (void *) &i);` – ouah Jan 03 '15 at 10:58
  • @ouah ,GCC does not give any warnings without the cast. Also,I get the same output if I had/had not added the cast. Is the cast compulsory? – Spikatrix Jan 03 '15 at 11:01
  • gcc warns with `-pedantic`, and yes the cast is required. – ouah Jan 03 '15 at 11:04
  • @ouah Sources, please. – Qix - MONICA WAS MISTREATED Jan 03 '15 at 11:04
  • @ouah , No. Even with `-pedantic`,GCC does not emit any error or warning – Spikatrix Jan 03 '15 at 11:08
  • 1
    @Qid Pointers are not "`void *` by nature". The cast is required. `int *` and `void *` are not guaranteed by the Standard to have same size or representation. For `p` conversion specifier, read C11, 7.21.6.1p8 "p The argument shall be a pointer to void". You are violating a shall -> undefined behavior. – ouah Jan 03 '15 at 11:10
  • @CoolGuy try with `-Wall -pedantic` – ouah Jan 03 '15 at 11:11
  • @ouah , Nothing. I compiled with `gcc filename -o blah -Wall -Wextra -std=c11 -pedantic` – Spikatrix Jan 03 '15 at 11:12
  • @ouah Huh, interesting. – Qix - MONICA WAS MISTREATED Jan 03 '15 at 11:12
  • @Qix , You can [read this](http://stackoverflow.com/questions/24867814/printfp-and-casting-to-void) – Spikatrix Jan 03 '15 at 11:16
  • @CoolGuy I blame your test case, at least all gcc recent versions warn with `-Wall -pedantic`. – ouah Jan 03 '15 at 11:17
  • @ouah , My GCC version is 4.8.1. Ok.You are right. I [tested it on an online compiler](http://rextester.com/runcode) and it gives a warning. Why is it not emitting a warning for me?? – Spikatrix Jan 03 '15 at 11:25
  • @ouah , Got the problem. When I use `-std=c99` or `-std=c11` or `-std=c89` or do not have an `-std` flag, there is no warning whereas when I use `-std=gnu89` or `-std=gnu99` or `-std=gnu11` ,GCC emits a warning. Why is it like that? – Spikatrix Jan 03 '15 at 11:30
  • @CoolGuy I got warning with `-std=c99` with both gcc 4.7.1 and 4.8.2. – ouah Jan 03 '15 at 11:34
  • 1
    @CoolGuy: MinGW? Maybe have a look at http://stackoverflow.com/questions/27363795/mingw-doesnt-produce-warnings – mafso Jan 03 '15 at 13:26
  • @mafso , [I think so](http://stackoverflow.com/questions/27755356/gcc-does-not-emit-a-warning-when-compiling).. – Spikatrix Jan 03 '15 at 14:14
4
printf("%p",i);

Using wrong format specifier will lead to undefined behavior. %p expects the arguement of type void * in your case you are passing the value of i and not the address in order to print out the address you should pass &i

You should use

printf("%d",i);

to print the integer value of i

The code should be like

#include<stdio.h>
void main()
{
   int i = 5;
   printf("%d",i); /* To print value of i */
   printf("%p",(void *)&i); /* Address of variable i */
}
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 1
    `p` requires a `(void *)` argument. You have to use `printf("%p", (void *) &i);` – ouah Jan 03 '15 at 10:58
  • @ouah Yes i have mentioned that in my answer casting `void *` looks redundant though don't you feel so? – Gopi Jan 03 '15 at 10:59
  • 1
    The cast in `printf("%p",(void *)&i);` is not redundant, it is required as `&i` is not of type `void *`. – ouah Jan 03 '15 at 11:12