格式'%d'需要'int'类型的参数,但参数2的类型为'int *'

时间:2014-02-12 00:24:41

标签: c printf

我正在使用Linux,当我尝试使用此代码显示时:

#include <stdio.h>
#include <math.h>

int main()
{
    int a,b,c;
    int result;

    printf( "How Many Times Would You Like to Add: ");
    scanf( "%d", &a );
    //getchar();

    printf( "What Number Would You Like to Add: ");
    scanf( "%d", &b );
    //getchar();

    printf( "What Number Would You Like to Add to It?: ");
    scanf( "%d", &c );
    //getchar();

    for( int $i = 0; $i < a; $i++ )
    {
        result = b + c;
        printf( "%d", &result );
        printf( "\n" );

    }

    return 0;
}

我明白了:

main.c: In function ‘main’:
main.c:26:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]

使用此编译命令:

cd ./TSTS
gcc -std=c99 main.c -o LetsADD!

1 个答案:

答案 0 :(得分:5)

for( int $i = 0; $i < a; $i++ )

在这里删除$。这不是PHP。

    printf( "%d", &result );

在这里删除&printf取值,而不是指针。 &获取变量的地址。 (请注意,scanf 接受指针,因为C使用按值传递,而scanf需要修改该值。)

相关问题