1

I want to output the results of several arithmetic operations using printf. First I read some data using scanf and perform calculations on those values.

Here is my code:

int main() {
    int n,a,b,c,e,f;

    scanf("%d%d%d%d%d%d", &n, &a, &b, &c, &f, &e);

    printf("Addition of (%d+%d) = %d\n", n, a, (n+a));
    printf("Substraction of (%d-%d) = %d\n", n, b, (n-b));
    printf("Multiplication of (%d*%d) = %d\n", n, c, (n*c));
    printf("Reminder of (%d%%d) = %d\n", n, f, (n%f));
    printf("Quotient of (%d/%d) = %d", n, e, (n/e));

    return 0;
}

However, when I try to compile that source I obtain the error message:

Solution.c:14:12: warning: too many arguments for format [-Wformat-extra-args]
     printf("Reminder of (%d%%d) = %d\n",n,f,(n%f));
Gerhard
  • 6,850
  • 8
  • 51
  • 81
  • 2
    dupe of [How to escape the % (percent) sign in C's printf?](https://stackoverflow.com/questions/1860159/how-to-escape-the-percent-sign-in-cs-printf) – underscore_d Apr 15 '21 at 10:18
  • 1
    In a `printf()` format string, `%%` outputs a single `%`. So the `%%d` in the `printf("Reminder of (%d%%d) = %d\n",n,f,(n%f))` outputs a `%` (`%%`) then the letter `d`. Whereas you're question suggests you incorrectly expect it to output a `%` followed by an `int`. The net effect is that format string tells `printf()` to expect two arguments of type `int` following the format string, while you have supplied three. Hence the warning. (Bear in mind that the standard does not *require* a diagnostic - you're getting lucky if your compiler does) – Peter Apr 15 '21 at 10:28
  • This doesn't address the question, but you don't need those parentheses around the calculations. `printf("Addition of (%d+%d) = %d\n",n,a,n+a);` will work just fine. – Pete Becker Apr 15 '21 at 14:12

0 Answers0