Home > Back-end >  Getting this error: warning: too many arguments for format [-Wformat-extra-args]
Getting this error: warning: too many arguments for format [-Wformat-extra-args]

Time:02-13

#include <stdio.h>

int main() {
  int r, c, a[100][100], b[100][100], sum[100][100], i, j;
  printf("Enter the number of rows: ");
  scanf("%d", &r);
  printf("Enter the number of columns: ");
  scanf("%d", &c);

  printf("\nEnter Matrix A\n", i   1, j   1);
  for (i = 0; i < r;   i)
    for (j = 0; j < c;   j) {
      scanf("%d", &a[i][j]);
    }

  printf("\nEnter Matrix B\n", i   1, j   1);
  for (i = 0; i < r;   i)
    for (j = 0; j < c;   j) {
      scanf("%d", &b[i][j]);
    }


  for (i = 0; i < r;   i)
    for (j = 0; j < c;   j) {
      sum[i][j] = a[i][j]   b[i][j];
    }

  printf("\nA   B = \n");
  for (i = 0; i < r;   i)
    for (j = 0; j < c;   j) {
      printf("%d   ", sum[i][j]);
      if (j == c - 1) {
        printf("\n\n");
      }
    }

  return 0;
}

When I compile this, I get an error message saying main.c:9:10: warning: too many arguments for format [-Wformat-extra-args] on lines 10 and 16. I’m new to programming C so any tips would be helpful.

CodePudding user response:

Oh I see you are just missing the %i for your printf. No big deal. That what it was telling you. If this helps please give green check. Thank you

#include <stdio.h>

int main() {
  int r, c, a[100][100], b[100][100], sum[100][100], i, j;
  printf("Enter the number of rows: ");
  scanf("%d", &r);
  printf("Enter the number of columns: ");
  scanf("%d", &c);

  printf("\nEnter Matrix A\n %i,%i", i   1, j   1);
  for (i = 0; i < r;   i)
    for (j = 0; j < c;   j) {
      scanf("%d", &a[i][j]);
    }

  printf("\nEnter Matrix B\n %i,%i", i   1, j   1);
  for (i = 0; i < r;   i)
    for (j = 0; j < c;   j) {
      scanf("%d", &b[i][j]);
    }


  for (i = 0; i < r;   i)
    for (j = 0; j < c;   j) {
      sum[i][j] = a[i][j]   b[i][j];
    }

  printf("\nA   B = \n");
  for (i = 0; i < r;   i)
    for (j = 0; j < c;   j) {
      printf("%d   ", sum[i][j]);
      if (j == c - 1) {
        printf("\n\n");
      }
    }

  return 0;
}

CodePudding user response:

Problem in the code: You are giving extra arguments in the printf() function without specifying format specifier. The syntax of printf() is int printf ( const char * format, ... );

You can check official or this websites to understand details of how to use printf() function.

Solution to your problem: At line 10 and 16 you can use %d format specifier for two arguments. The code will be printf("\nEnter Matrix A: %d, %d\n", i 1, j 1);

However, you should not use those lines to imply matrix dimensions. You can write something like printf("\nEnter Matrix A: which is (%d by %d) matrix\n", r, c); to denote matrix dimensions.

  • Related