Type System and Value Category in the Conditional Operator (?:)
In C , the conditional operator (?:) evaluates to a value of a specific type and value category, which defines whether it can refer to an object in memory or not.
In the first example provided:
int x = 1; int y = 2; (x > y ? x : y) = 100;
Both x and y are int variables, and the condition x > y is false. Therefore, y is assigned the value of 100. Since the type of both operands is the same, the conditional expression itself becomes an lvalue, which means it can be assigned to.
However, in the second example:
int x = 1; long y = 2; (x > y ? x : y) = 100;
x is an int and y is a long, making the types of the operands different. To compare x and y, a conversion is required, which results in the conditional expression becoming an rvalue rather than an lvalue. Since an rvalue cannot be assigned to, the assignment in this case is invalid.
To summarize, a conditional expression is an lvalue if the second and third operands are lvalues of the same type, allowing assignment to the result. If they have different types, the conditional expression becomes an rvalue, which cannot be assigned to.
The above is the detailed content of How Does C 's Conditional Operator (?:) Determine Lvalue or Rvalue Based on Operand Types?. For more information, please follow other related articles on the PHP Chinese website!