Reputation: 65
I wrote simple C program with scanf & printf like:
int n;
scanf("%d", &n);
int result = 7 - n;
printf("%d", &result);
and got this warning message:
warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=] printf("%d", &result);
I dont understand why argument 2 has type int * instead of int? How can I solve this?
Upvotes: 0
Views: 1206
Reputation: 11921
result
is a integer variable. If you want to print its value then use %d
format specifier & provide the argument as only result
not &result
.
This
printf("%d", &result);
replace with
printf("%d", result);
If you want to print the address of result
variable then use %p
format specifier.
printf("%p", &result); /* printing address */
Edit : %p
format specifier needs an argument of void*
type.
So to print the address of result
cast it as void*
. for e.g
printf("%p", (void*)&result); /* explicitly type casting to void* means it works in all cases */
Thanks @ajay for pointing that, I forgot to add this point.
Upvotes: 2