The first argument to scanf must be a pointer to a string. Most often it is given as a string literal, such as "%c", which is automatically converted to a pointer to its first element.
'%c' is not a string or a pointer to a string. It is a multicharacter constant, which is effectively an int constant.
Enable warnings in your compiler and elevate warnings to errors. With Clang, start with -Wmost -Werror. With GCC, start with -Wall -Werror. With MSVC, start with /W3 /WX.
发布评论
评论(3)
而不是此调用中的格式字符串
您使用具有类型
int
的多重整数字符常数, 。因此,呼叫调用了未定义的行为。您还需要使用字符串字面的字符串
,也最好在格式字符串中包含一个领先空间,
因此可以跳过输入缓冲区中的白空间字符。
Instead of the format string in this call of
scanf
you are using a multibyte integer character constant that has the type
int
. So the call invokes undefined behavior.You need to use a string literal
Also it is better to include a leading space in the format string like
This allows to skip white space characters in the input buffer.
AS eric 说,您必须提供字符串(请参阅参考)
int scanf(const char * format,...);
以下一个应解决您的问题:
'%c'
带有“%c”
,因此代码应该看起来像int getchar(void);
)As Eric said, you have to provide string (see reference)
int scanf ( const char * format, ... );
One of the following should solve your problem:
'%c'
with"%c"
so the code should look like thisint getchar ( void );
)scanf
的第一个参数必须是指向字符串的指针。通常,它以字符串字面形式给出,例如“%c”
,它会自动转换为指向其第一个元素的指针。'%c'
不是字符串或指针。它是一个多截面常数,实际上是int
常数。在编译器中启用警告,并将警告提升到错误。使用clang,以
- waste -werror
开始。使用GCC,以-wall -werror
开始。使用MSVC,以/w3/wx
开始。The first argument to
scanf
must be a pointer to a string. Most often it is given as a string literal, such as"%c"
, which is automatically converted to a pointer to its first element.'%c'
is not a string or a pointer to a string. It is a multicharacter constant, which is effectively anint
constant.Enable warnings in your compiler and elevate warnings to errors. With Clang, start with
-Wmost -Werror
. With GCC, start with-Wall -Werror
. With MSVC, start with/W3 /WX
.