""" Sample python code representing multiple assignment """
a , b = 0 , 1
print a , b
The following code gives output : 0 1 and obviously does not raise any error. Does C support the same?
""" Sample python code representing multiple assignment """
a , b = 0 , 1
print a , b
The following code gives output : 0 1 and obviously does not raise any error. Does C support the same?
No C does not support multiple assignments like this.
Compilation passes since a , b = 0 , 1
is grouped as a, (b = 0), 1
. a
and 1
are no-ops but still valid expressions; the expression is equivalent to
b = 0
with a
not changed.
Interestingly, you can achieve your desired notation in C++ with some contrivance and a minor change in the syntax.
C does not support list assignments as Python does. You need to assign to each variable separately:
a = 0; b = 1;
No, C does not support multiple-assignment, nor has it language-level support for tuples.
a, b = 0, 1;
The above is, considering to operator precedence, equivalent to:
a, (b = 0), 1;
Which is equivalent to:
b = 0;
See live on coliru.
The closest C-equivalent to your Python code would be:
a = 0, b = 1;
In this case, using the value of an assignment-expression isn't useful.