Answer the question
In order to leave comments, you need to log in
Why does the compiler swear?
Hello. I recently learned that there is a "shortened version" of if...else.
If the user enters the letter 'h', then high is set to the value of guess, otherwise low = guess.
However, the compiler writes this: error: lvalue required as left operand of assignment. But if I put the parentheses (i.e. (high = guess) : (low = guess), then everything becomes normal. Probably because : ? has higher precedence than assignment, but I don’t know how exactly. Please explain that not this way
ch == 'h' ? high = guess : low = guess;
Answer the question
In order to leave comments, you need to log in
According to the C standard, it evaluates to logical-OR-expression ? expression : conditional-expression - this means that the second operand can be any expression (even using the , operator), and the third operand is subject to precedence logic and must be greater than or equal to the ternary operator. In this case, this evaluates to ((ch == 'h') ? (high = guess) : low) = guess. The ?: operator returns a temporary value, and the compiler says so.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question