C编译器警告"警告:格式参数太多"怎么修?

时间:2017-07-22 11:39:21

标签: c eclipse printf mingw compiler-warnings

我刚开始进行C编程,并且正在使用Eclipse Mars环境和MinGW编译器。编写一个添加两个整数并输出总和的程序时遇到了问题。

我收到了"警告:格式"以下语句的对话框:

printf("Sum of %d ", integer1," and %d", integer2," is: %d\n", sum);

任何人都可以解释为什么这是不正确的以及我如何纠正它?

(完整的程序如下):

#include <stdio.h>
#include <stdlib.h> 

int main(){

    int integer1; 
    int integer2;

    printf("Enter first integer \n"); 
    scanf("%d\n", &integer1);

    printf("Enter second integer \n"); 
    scanf("%d", &integer2); 

    int sum; 
    sum = integer1 + integer2;

    printf("Sum of %d ", integer1," and %d\a", integer2," is: %d\n", sum);

    system("pause");

    return 0;
}

任何人都可以解释如何重写不正确的陈述吗?

2 个答案:

答案 0 :(得分:4)

printf()  获取一个字符串,其中可能包含格式说明符,然后是参数列表:

printf("Sum of %d and %d\a is: %d\n", integer1, integer2, sum);

答案 1 :(得分:3)

函数printf具有以下声明

int printf(const char * restrict format, ...);

因此你应该写

printf("Sum of %d and %d\a is: %d\n", integer1, integer2, sum);
相关问题