C - 无法找出此错误 : "C2106: ' =' : left operand must be l-value"

标签 c

我正在使用 Visual Studio 2013 来尝试学习 C 语言。

我正在使用著名的K&R book将书中的以下代码片段复制并粘贴到 Visual Studio 2013 后,我收到错误:

1 error C2106: '=' : left operand must be l-value
2 IntelliSense: expression must be a modifiable lvalue

#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
main()
{
    int c, nl, nw, nc, state;
    state = OUT;
    nl = nw = nc = 0;

    while ((c = getchar()) != EOF) {
        ++nc;
        if (c == '\n')
            ++nl;
        if (c == ' ' || c == '\n' || c = '\t')
            state = OUT;
        else if (state == OUT) 
            state = IN;
            ++nw;
    }
    printf("%d %d %d\n", nl, nw, nc);
}

看来错误来自于以下语句:

if (c == ' ' || c == '\n' || c = '\t')

但我真的不知道出了什么问题,更不用说代码是直接来自书中的事实了。

任何帮助将不胜感激!

谢谢!

最佳答案

if (c == ' ' || c == '\n' || c = '\t')
//                             ^ Oops.

建议你休息一下。即使不知道分配会尝试写入 || 的结果,因为它的优先级较低,但差异还是很大。

关于C - 无法找出此错误 : "C2106: ' =' : left operand must be l-value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28256556/

相关文章:

CMake 不会创建目标文件并且无法链接

关闭客户端中的套接字会使 nodejs 服务器崩溃

C串口程序使用键盘和显示器代替串口/dev/ttyUSBX

python - 整数加一和三元运算符

c++ - 条件编译的注意事项

C - 这个答案的解释

c - linux clone() 返回 -1 作为 child_pid

c - 从输出接口(interface)以外的另一个接口(interface)选择 IP 源地址

c++ - 如何将可变数量的参数传递给 printf/sprintf

c - 在C中的不同文件中使用char