繁体   English   中英

如何使用此“printf”函数减少参数数量? 我应该使用“printf”以外的其他东西吗?

[英]How can I reduce the number of arguments with this "printf" function? Should I be using something other than "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);

    }
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM