#include <stdio.h>
int main()
{
int a=3,b=4,g;
a > b ? g = a : g = b ;
printf("%d",g);
return 0;
}
为什么值g
没有打印出来?编译器说lvalue
必需的。这是什么意思?
由于运算符的优先级较高?:
over =
, 表达方式
a > b ? g = a : g = b;
将被解析为
(a > b ? g = a : g) = b;
The expression (a > b ? g = a : g)
will give an rvalue. The left operand of assignment (=
) operator must be an lvalue1 (modifiable2).
Change
a > b ? g = a : g = b ;
to
a > b ? (g = a) : (g = b);
or
g = a > b ? a : b;
1. C11-§6.5.16/2: An assignment operator shall have a modifiable lvalue as its left operand.
2. §6.3.2.1/1: An lvalue is an expression (with an object type other than void) that potentially designates an object;64) if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const- qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const- qualified type.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)