Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple assignment in one line

Tags:

c

embedded

I just come across the statement in embedded c (dsPIC33)

sample1 = sample2 = 0; 

Would this mean

sample1 = 0;  sample2 = 0; 

Why do they type it this way? Is this good or bad coding?

like image 539
riscy Avatar asked Oct 14 '13 04:10

riscy


People also ask

What is multi assignment?

multiple assignment A form of assignment statement in which the same value is given to two or more variables. For example, in Algol, a := b := c := 0. sets a, b, c to zero. A Dictionary of Computing. "multiple assignment ."

What is multiple assignment in C?

MultipleAssignment is a language property of being able to assign one value to more than one variables. It usually looks like the following: a = b = c = d = 0 // assigns all variables to 0.

What is multiple assignment operator?

All types of assignment operators can be mixed in a multiple assignment statement. All varieties of assignment operator have the same precedence, lower than that of any other Java operator. As with simple assignments, evaluation again takes place from right to left.

What is multiple assignment in Python?

Python allows you to assign a single value to several variables simultaneously. For example − a = b = c = 1. Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. You can also assign multiple objects to multiple variables.


2 Answers

Remember that assignment is done right to left, and that they are normal expressions. So from the compilers perspective the line

sample1 = sample2 = 0; 

is the same as

sample1 = (sample2 = 0); 

which is the same as

sample2 = 0; sample1 = sample2; 

That is, sample2 is assigned zero, then sample1 is assigned the value of sample2. In practice the same as assigning both to zero as you guessed.

like image 180
Some programmer dude Avatar answered Sep 22 '22 14:09

Some programmer dude


Formally, for two variables t and u of type T and U respectively

T t; U u; 

the assignment

t = u = X; 

(where X is some value) is interpreted as

t = (u = X); 

and is equivalent to a pair of independent assignments

u = X; t = (U) X; 

Note that the value of X is supposed to reach variable t "as if" it has passed through variable u first, but there's no requirement for it to literally happen that way. X simply has to get converted to type of u before being assigned to t. The value does not have to be assigned to u first and then copied from u to t. The above two assignments are actually not sequenced and can happen in any order, meaning that

t = (U) X; u = X; 

is also a valid execution schedule for this expression. (Note that this sequencing freedom is specific to C language, in which the result of an assignment in an rvalue. In C++ assignment evaluates to an lvalue, which requires "chained" assignments to be sequenced.)

There's no way to say whether it is a good or bad programming practice without seeing more context. In cases when the two variables are tightly related (like x and y coordinate of a point), setting them to some common value using "chained" assignment is actually perfectly good practice (I'd even say "recommended practice"). But when the variables are completely unrelated, then mixing them in a single "chained" assignment is definitely not a good idea. Especially if these variables have different types, which can lead to unintended consequences.

like image 43
AnT Avatar answered Sep 26 '22 14:09

AnT