In the following code:
int c;
while((c=10)>0)
What does c = 10
evaluate to? Is it 1 which indicates that the value 10 is assigned to variable c successfully, or is it 10? Why?
In most expression-oriented programming languages (for example, C), the assignment statement returns the assigned value, allowing such idioms as x = y = a , in which the assignment statement y = a returns the value of a , which is then assigned to x .
An assignment expression has the value of the left operand after the assignment, but is not an lvalue.
The assignment operator = assigns the value of its right-hand operand to a variable, a property, or an indexer element given by its left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand.
c = 10 is an expression returning 10 which also assigns 10 to c.
It is said in C99 6.5.16
An assignment operator stores a value in the object designated by the left operand. An
assignment expression has the value of the left operand after the assignment, but is not an
lvalue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With