I have :
我有 :
#include<stdio.h>
int main()
{
int a=5,b=6;
(a>b)?b=a:b=b; // Here is the error
return 0;
}
But if I replace :
但如果我更换:
(a>b)?b=a:b=b; // Error
with
(a>b)?(b=a):(b=b); // No-Error
I understand the lvalue
is a value to which something can be assigned and how is it different from rvalue
, but why is the extra parenthesis
making the difference.
我理解左值是一个可以赋值的值以及它与右值有什么不同,但为什么额外的括号会产生差异。
4 个解决方案
#1
8
Assignment has a lower precedence than the ternary operator so the line evaluates like:
赋值的优先级低于三元运算符,因此该行的计算结果如下:
((a>b)?b=a:b)=b;
use:
b=(a>b)?a:b;
#2
18
Actually, in C, this code
实际上,在C中,这段代码
(a>b)?b=a:b=b;
is parsed by many compilers as
由许多编译器解析为
((a>b)?b=a:b)=b;
which is an error, as the expression ((a>b)?b=a:b)
evaluates to an rvalue which you try to assign with b
which results in an error. Trying to assign an rvalue is an error. If it is not parsed that way, then its simply a syntax error. But a C compiler is NOT allowed to parse it as:
这是一个错误,因为表达式((a> b)?b = a:b)求值为你尝试用b赋值的右值,这会导致错误。尝试分配右值是一个错误。如果没有这样解析,那么它只是一个语法错误。但是不允许C编译器将其解析为:
((a>b)?b=a:(b=b)); //not allowed to parse by C language
Because the grammar of C does not allow a compiler to parse the code as above.
因为C的语法不允许编译器解析上面的代码。
But what you've written (the original code) is correct as C++.
但你写的(原始代码)是正确的C ++。
Here the grammars of C and C++ differ a lot. And because of that difference you see both languages treat the expression differently. That is, the conditional expression in C++
is different from the conditional expression in C
.
这里C和C ++的语法差别很大。由于存在这种差异,您会看到两种语言对表达的区别对待。也就是说,C ++中的条件表达式与C中的条件表达式不同。
Wikipedia has very good and correct explanation for this:
*对此有非常好的和正确的解释:
The binding of operators in C and C++ is specified (in the corresponding Standards) by a factored language grammar, rather than a precedence table. This creates some subtle conflicts. For example, in C, the syntax for a conditional expression is:
C和C ++中的运算符绑定是通过因式语言语法(而不是优先级表)指定的(在相应的标准中)。这会产生一些微妙的冲突。例如,在C中,条件表达式的语法是:
logical-OR-expression ? expression : conditional-expression
逻辑OR表达?表达式:条件表达式
while in C++ it is:
而在C ++中它是:
logical-OR-expression ? expression : assignment-expression
逻辑OR表达?表达式:赋值表达式
Hence, the expression:
因此,表达式:
e = a < d ? a++ : a = d
e = a
?> is parsed differently in the two languages. In C, this expression is a syntax error, but many compilers parse it as:
在两种语言中解析不同。在C中,此表达式是语法错误,但许多编译器将其解析为:
e = ((a < d ? a++ : a) = d)
e =((a
?a> which is a semantic error, since the result of the conditional-expression (which might be a++) is not an lvalue. In C++, it is parsed as:
这是一个语义错误,因为条件表达式(可能是++)的结果不是左值。在C ++中,它被解析为:
e = (a < d ? a++ : (a = d))
e =(a
?a> which is a valid expression.
这是一个有效的表达。
#3
2
It is really:
它确实是:
((a>b)?b=a:b)=b;
Note: you should simply
注意:你应该简单
b = (a>b)?a:b;
#4
0
When we put an equation in parenthesis it is treated as an expression. And it returns some value which provide solution to the error.
当我们在括号中放入一个方程时,它被视为一个表达式。它返回一些值,为错误提供解决方案。
#1
8
Assignment has a lower precedence than the ternary operator so the line evaluates like:
赋值的优先级低于三元运算符,因此该行的计算结果如下:
((a>b)?b=a:b)=b;
use:
b=(a>b)?a:b;
#2
18
Actually, in C, this code
实际上,在C中,这段代码
(a>b)?b=a:b=b;
is parsed by many compilers as
由许多编译器解析为
((a>b)?b=a:b)=b;
which is an error, as the expression ((a>b)?b=a:b)
evaluates to an rvalue which you try to assign with b
which results in an error. Trying to assign an rvalue is an error. If it is not parsed that way, then its simply a syntax error. But a C compiler is NOT allowed to parse it as:
这是一个错误,因为表达式((a> b)?b = a:b)求值为你尝试用b赋值的右值,这会导致错误。尝试分配右值是一个错误。如果没有这样解析,那么它只是一个语法错误。但是不允许C编译器将其解析为:
((a>b)?b=a:(b=b)); //not allowed to parse by C language
Because the grammar of C does not allow a compiler to parse the code as above.
因为C的语法不允许编译器解析上面的代码。
But what you've written (the original code) is correct as C++.
但你写的(原始代码)是正确的C ++。
Here the grammars of C and C++ differ a lot. And because of that difference you see both languages treat the expression differently. That is, the conditional expression in C++
is different from the conditional expression in C
.
这里C和C ++的语法差别很大。由于存在这种差异,您会看到两种语言对表达的区别对待。也就是说,C ++中的条件表达式与C中的条件表达式不同。
Wikipedia has very good and correct explanation for this:
*对此有非常好的和正确的解释:
The binding of operators in C and C++ is specified (in the corresponding Standards) by a factored language grammar, rather than a precedence table. This creates some subtle conflicts. For example, in C, the syntax for a conditional expression is:
C和C ++中的运算符绑定是通过因式语言语法(而不是优先级表)指定的(在相应的标准中)。这会产生一些微妙的冲突。例如,在C中,条件表达式的语法是:
logical-OR-expression ? expression : conditional-expression
逻辑OR表达?表达式:条件表达式
while in C++ it is:
而在C ++中它是:
logical-OR-expression ? expression : assignment-expression
逻辑OR表达?表达式:赋值表达式
Hence, the expression:
因此,表达式:
e = a < d ? a++ : a = d
e = a
?> is parsed differently in the two languages. In C, this expression is a syntax error, but many compilers parse it as:
在两种语言中解析不同。在C中,此表达式是语法错误,但许多编译器将其解析为:
e = ((a < d ? a++ : a) = d)
e =((a
?a> which is a semantic error, since the result of the conditional-expression (which might be a++) is not an lvalue. In C++, it is parsed as:
这是一个语义错误,因为条件表达式(可能是++)的结果不是左值。在C ++中,它被解析为:
e = (a < d ? a++ : (a = d))
e =(a
?a> which is a valid expression.
这是一个有效的表达。
#3
2
It is really:
它确实是:
((a>b)?b=a:b)=b;
Note: you should simply
注意:你应该简单
b = (a>b)?a:b;
#4
0
When we put an equation in parenthesis it is treated as an expression. And it returns some value which provide solution to the error.
当我们在括号中放入一个方程时,它被视为一个表达式。它返回一些值,为错误提供解决方案。