Ste*_*ang 0 c printf scanf c-strings char
我正在尝试创建一个颜色菜单,但最后一行报告了一些错误。
puts("I don't know about the color %c",input);
Run Code Online (Sandbox Code Playgroud)
这是声明
char input[7];
Run Code Online (Sandbox Code Playgroud)
和初始化
scanf("%c",&input);
Run Code Online (Sandbox Code Playgroud)
错误在这里
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);
^~~~
Run Code Online (Sandbox Code Playgroud)
为什么?
首先这个电话
scanf("%c",&input);
^^^ ^^^
Run Code Online (Sandbox Code Playgroud)
是不正确的。你必须写
scanf("%s", input);
^^^ ^^^
Run Code Online (Sandbox Code Playgroud)
至于错误消息,则该函数puts只接受一个参数。看来您指的是函数printf而不是puts. 如果您要输出整个字符串%s,%c则格式说明符应为而不是。
printf("I don't know about the color %s",input);
^^^
Run Code Online (Sandbox Code Playgroud)
否则,如果您只想输入一个字符而不是字符串,那么您需要编写
scanf("%c",input);
Run Code Online (Sandbox Code Playgroud)
和
printf("I don't know about the color %c\n",input[0]);
Run Code Online (Sandbox Code Playgroud)