cvariable-assignmentmultiple-assignment

Does C support multiple assignment of the type a , b = 0, 1 like python does?


""" 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?


Solution

  • No, C does not support multiple-assignment, nor has it language-level support for tuples.

    a, b = 0, 1;
    

    The above is, considering operator precedence, equivalent to:

    a, (b = 0), 1;
    

    Which is equivalent to:

    b = 0;
    

    The closest C-equivalent to your Python code would be:

    a = 0, b = 1;
    

    That would be needlessly complex and confusing though, thus prefer separating it into two statements for much cleaner code:

    a = 0;
    b = 1;