0
votes

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?

1
You just give it result, not address of result.Austin Stephens
The warning message says all necessary. Read it and manual for printf before posting here.i486

1 Answers

2
votes

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.