简体   繁体   中英

How can I reduce the number of arguments with this "printf" function? Should I be using something other than "printf"?

I'm having an issue that I've been attempting to solve. I understand the difference between printf and puts, so I figured that printf was the correct way to go here.

However, I get an error that says:

"warning: too many arguments for format [-Wformat-extra-args]

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

Is there a better function to print the output I need or perhaps a different way to format this one?

#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);

    }
}

Your format string must be the first argument of printf and your inputs must be the following ones. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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