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

前端 未结 3 1342
野的像风
野的像风 2021-01-12 19:56

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 argume         


        
3条回答
  •  梦毁少年i
    2021-01-12 20:11

    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.

提交回复
热议问题