Search code examples
ccharprintfscanfc-strings

too many arguments in function call in c?


I'm trying to create a menu of colors but the last line reported some error.

puts("I don't know about the color %c",input);

Here's the declaration

char input[7];

And the initialization

scanf("%c",&input);

The error is here

Too many arguments in function call
error: expected declaration specifiers or '...' before '&' token
scanf("%c",&input);
        ^
error: expected declaration specifiers or '...' before string constant
scanf("%c",&input);
   ^~~~

Why is it?


Solution

  • For starters this call

    scanf("%c",&input);
          ^^^ ^^^
    

    is incorrect. You have to write

    scanf("%s", input);
          ^^^  ^^^
    

    As for the error message then the function puts accepts only one argument. It seems you mean the function printf instead of puts. And the format specifier shall be %s instead of %c if you are going to output a whole string.

    printf("I don't know about the color %s",input);
                                         ^^^
    

    Otherwise if you was going to input only one character instead of a string then you need to write

    scanf("%c",input);
    

    and

    printf("I don't know about the color %c\n",input[0]);