c - (c) 表达式必须是可修改的左值

标签 c if-statement lvalue

if (operation = '+' || operation = '-' || operation = '*' || operation = '/' || operation = '%')//error line
    {
        printf("Enter the first operand:\t\t");
        getchar();
        scanf("%d", &num1);
        printf("Enter the second operand:\t\t");
        getchar();
        scanf("%d", &num2);
}

它给出了一个错误说:

Error: expresion must be a modifiable value

它给了我关于那条 if 行的错误,关于所有的参数,除了那个说的那个

operation = '%'

问题是什么?? 谢谢你们:)

最佳答案

你打错了,你写了赋值运算符 = 而不是比较运算符 ==

代替

if (operation = '+' || operation = '-' || operation = '*' || operation = '/' || operation = '%')

必须有

if (operation == '+' || operation == '-' || operation == '*' || operation == '/' || operation == '%')

您可以将 if 语句写得更简单。例如

const char *Operations = "+-*/%";

char *p = strchr( Operations, operation );

if ( p !=  NULL )
{
        printf("Enter the first operand:\t\t");
        getchar();
        scanf("%d", &num1);
        printf("Enter the second operand:\t\t");
        getchar();
        scanf("%d", &num2);
}

甚至不声明指针 p

const char *Operations = "+-*/%";

if ( strchr( Operations, operation ) !=  NULL )
{
        printf("Enter the first operand:\t\t");
        getchar();
        scanf("%d", &num1);
        printf("Enter the second operand:\t\t");
        getchar();
        scanf("%d", &num2);
}

关于c - (c) 表达式必须是可修改的左值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26945802/

相关文章:

c - 可靠的 shellcode 测试

c - 排列方程中的项以隔离 C 中的 y 项

javascript - 如果选中,如何更改 <option> 的输出值?

c++ - 为什么内置赋值返回一个非 const 引用而不是 C++ 中的 const 引用?

c++ - 多维数组在循环中赋值 : expression must be a modifiable lvalue

c++ - 右值引用被视为左值?

c - 使用 MSBuild 的 CL 任务不会创建可执行文件

c - Windows 是否有一个好的 Valgrind 替代品?

c++ - 警告C6236:(||)始终为非零常量

javascript - 使用 Javascript 验证第二个数组中的值是否对应于第一个数组的平方值的函数