第10行的左值误差

时间:2015-04-02 16:30:13

标签: c compiler-errors lvalue

#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错误,请帮助。这是我的第一个有史以来的节目,这是我编程的第一天,我是自学。

3 个答案:

答案 0 :(得分:1)

答案 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.
}