warning: too many arguments for format

Rendered by QuickLaTeX.com

The naked, printf function will have the first argument as a format string, and, the second argument as a list of variables

in the first place, always make sure that the “format string”  match up properly  with the list of variables by number and type  

on the whole, The format string contains two types of objects

  • Characters[d, s, p, i, f, e, E, O, G, g]
  • Conversion specifications(%)

to sum up, they do conversion and printing of the next argument for the printf function

in short, each conversion specification begins with % and ends with the conversion character

to point out, what causes the warning?

in reality, the printf or scanf function gets confused

but why?

To be sure, The printf or scanf function uses its first argument to decide two things

  • First, How many arguments
  • Second, What their types are

like it or not, If anything is missing in the first argument such as the wrong type, or if there are not enough arguments

without a doubt, you will get a wrong result

to be sure, Let’s deconstruct some examples

to begin with, Here’s an example, the printf function is missing the conversion specification(%) in the first argument which leads to a warning and wrong results

#include <stdio.h>

int main()
{
    int a=5;
    printf("d",a);
    return 0;
}

Here’s  another one, the scanf function is missing the conversion specification(%) in the first argument which leads to a warning and wrong results

#include <stdio.h>

int main()
{
    int a;
    printf("Enter a num value here");
    scanf("d",&a);
    printf("%d",a);
    return 0;
}

As can be seen, from the below program the printf function has two variables a and b in the first argument

at this time, we have only one format string

without a doubt, which is not matching with the two declared variables

#include <stdio.h>

int main()
{
    int a=5;
    int b=6;
    printf("%d",a,b);
    return 0;
}

comparatively, some safety tips to keep in mind

as can be seen, the format string doesn’t match the list of variables

int a=5;
    printf("%d%d",a); // wrong result

as i have said, format string must match in number with the list of variables

 int a=5;
    int b=6;
    printf("%d",a,b); // wrong result

a is int and b is float but the format string is in the wrong order

int a=5;
    float b=6;
    printf("%f%d",a,b); // wrong result

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.