[英](invalid lvalue in assignment) this error happens when i run it.what does it mean?
include<stdio.h>
main()
{
char m;
int a,b,n=0;
scanf("%c%d%d",&m,&a,&b);
m=='A' || m=='B' || m=='C' ? n=(3*a)+(5*b) : n=(5*a)+(3*b);
printf("%d\n",n);
}
Use instead 改用
m=='A' || m=='B' || m=='C' ? n=(3*a)+(5*b) : ( n=(5*a)+(3*b));
Otherwise the statement looks like 否则,语句看起来像
( m=='A' || m=='B' || m=='C' ? n=(3*a)+(5*b) : n)=(5*a)+(3*b);
Or you could write 或者你可以写
n = m=='A' || m=='B' || m=='C' ? (3*a)+(5*b) : (5*a)+(3*b);
The conditional operator in C is defined the following way C中的条件运算符通过以下方式定义
conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression
As the assignment operator has lower priority then the compiler issues an error because the assignment is excluded from the conditional operator for the third operand 由于赋值运算符的优先级较低,因此编译器将发出错误,因为该赋值已从第三个操作数的条件运算符中排除
The used by you expression would be valid in C++ because in C++ the operator is defined differently 您使用的表达式在C ++中将是有效的,因为在C ++中,运算符的定义不同
conditional-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
^^^^^^^^^^^^^^^^^^^^^
There is no need to use a complicated statement that confuses everyone, including the compiler. 无需使用会使所有人(包括编译器)困惑的复杂语句。 This is just as effective, and a lot easier to read:
这同样有效,而且更容易阅读:
if (m=='A' || m=='B' || m=='C')
n=(3*a)+(5*b);
else
n=(5*a)+(3*b);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.