Reputation: 63
While I'm compiling this simple program in GCC compiler I'm getting this error:- warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int’ [-Wformat=]*
#include <stdio.h>
int main()
{
printf("Pointer\n");
printf("*******\n\n");
int i=3;
printf("Address of Variable i : %u",&i);
printf("Value stored in Variable i : %d\n",i);
printf("Value stored in Variable i : %d\n",*(&i));
return 0;
}
Upvotes: 2
Views: 12917
Reputation: 74118
Even without any extra option, gcc (9.3.0) shows a detailed warning message
a.cpp: In function 'int main()':
a.cpp:7:38: warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'int*' [-Wformat=]
7 | printf("Address of Variable i : %u",&i);
| ~^ ~~
| | |
| | int*
| unsigned int
| %n
So there's a mismatch between the format %u
(unsigned int) and the argument &i
(pointer).
Looking at printf
Conversion specifier
u converts an unsigned integer into decimal representation dddd.
...
p writes an implementation defined character sequence defining a pointer.
The proper format in this case would be %p
for a pointer argument &i
printf("Address of Variable i : %p", &i);
Upvotes: 2