Search code examples
cformatting

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


  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]);
    }

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.


Solution

  • 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.