首页 > 解决方案 > 如何使用此“printf”函数减少参数数量?我应该使用“printf”以外的东西吗?

问题描述

我遇到了一个我一直在尝试解决的问题。我了解 printf 和 puts 之间的区别,所以我认为 printf 是正确的方法。

但是,我收到一条错误消息:

“警告:格式参数过多 [-Wformat-extra-args]

printf("%s", "The sum of %d", first , " and %d", second, " is %d", first + second);
^"

是否有更好的功能来打印我需要的输出,或者可能有不同的方式来格式化这个输出?

#include <stdio.h>

void main()
{
    int first, second;
    int answer = 1;

    while (answer == 1)
    {
        puts("Please enter the first integer ==> ");
        scanf("%d", &first);

        puts("Please enter the second integer ==> ");
        scanf("%d", &second);

        printf("%s", "The sum of %d", first , " and %d", second, " is %d", first + second);

        puts("Would you like to add two more integers?\n"
              "(1 for yes) ==> ");
              scanf("%d", &answer);

    }
}

标签: c

解决方案


您的格式字符串必须是的第一个参数,printf并且您的输入必须是以下的。例如:

printf("The sum of %d and %d is %d", first, second, first + second);

推荐阅读