Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the result of an assignment expression in C? [duplicate]

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?

like image 795
user2131316 Avatar asked May 15 '13 14:05

user2131316


People also ask

What does an assignment expression return in c?

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 .

What is the value of an assignment expression in c?

An assignment expression has the value of the left operand after the assignment, but is not an lvalue.

What is assignment operator expression?

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.


2 Answers

c = 10 is an expression returning 10 which also assigns 10 to c.

like image 74
Bathsheba Avatar answered Nov 15 '22 18:11

Bathsheba


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.
like image 37
Gurity Avatar answered Nov 15 '22 20:11

Gurity