#include <stdio.h>
#include <conio.h>
void main ()
{
clrscr () ;
char a [5];
puts ("K?");
gets (a);
fflush (stdin);
if (a = ("K"))
{
puts (a);
}
else
{
puts ("BAE");
}
getch ();
}
第10行在编译时显示Lvalue错误,请帮助。这是我的第一个有史以来的节目,这是我编程的第一天,我是自学。
答案 0 :(得分:1)
fflush(stdin)
是Undefined Behavior ==
; =
是分配运算符。您无法将任何内容分配给数组,因此会引发错误<string.h>
中的strcmp()
来比较字符串; ==
只是compares their addresses 答案 1 :(得分:0)
我认为而不是if语句中的赋值
if (a = ("K"))
你的意思是比较。
尽管如此,数组没有比较运算符。您必须自己逐个元素地比较数组。对于字符数组,标题strcmp
中声明了函数<string.h>
。
因此,有效代码看起来像
此外,您不应在标准输入中使用fflush功能。否则程序有未定义的行为,
#include <string.h>
//...
if ( strcmp( a, "K" ) == 0 )
//...
答案 2 :(得分:0)
这一行有两个错误
if (a = ("K"))
首先,您使用=
表示==
。但无论如何,这是错误的,你不能在C中测试字符串相等性。它必须是这样的,它测试字符串的第一个字符及其终结符
if (a[0] == 'K' && a[1] == '\0')
或者,您可以使用库函数
#include <string.h>
...
if (strcmp(a, "K") == 0) {
// are the same string.
}