Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’

Tags:

c

Each time I submit a program on hackerrank the following error occurs.

solution.c: In function ‘main’:
solution.c:22:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
 printf("%d", &sum);

It would be very helpful if someone could tell me what this means?

like image 244
akanksha kumari Avatar asked May 26 '17 08:05

akanksha kumari


1 Answers

I assume that you have declared sum as an int. So the correct call to printf is :

printf("%d", sum);

as %d specifier means that you are going to print an int, but you are passing the int's address, which is a pointer to int, or int *.


NOTE : Do not confuse printf with scanf, as the second does require a pointer. So, for reading variable sum, you would use :

scanf("%d", &sum);

but for printing, the correct way is without &, as written above.

like image 162
Marievi Avatar answered Sep 22 '22 16:09

Marievi