簡體   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