Search code examples
cvariable-assignmentcomma-operator

assigning tuple to an int variable and printing the variable


I tried assigning a tuple to an int variable. Here is my code. i = (12, 45, 58, -1, 90, 100); When I print i, it shows the value of 100. How does tuple work in C language ?

Thanks


Solution

  • The right hand side expression is an expression with the comma operator

    i = (12, 45, 58, -1, 90, 100);
    

    Its value and the type is the value and the type of the last operand.

    In fact the above statement is equivalent to

    i = 100;
    

    because other sub-expressions do not have a side effect.

    From the C Standard (6.5.17 Comma operator)

    2 The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.