-3

Consider the statement :

int a;
a = 1,2,3; 
cout<<a; // prints a = 1

I consulted this site. Now this happens because comma operator has the least precedence. So it is like (a=1),2,3. But out of curiosity, I want to know what happens after this, does the compiler forget about the remaining numbers 2 and 3. Because I think if they are considered then probably first 1 and then 2 and then 3 will get initialized to a (similar to a = (1,2,3)). Please tell me what exactly happens here?

gsamaras
  • 69,751
  • 39
  • 173
  • 279
Miraj50
  • 4,095
  • 1
  • 19
  • 33
  • When the compiler encounters the expression `2` and `3`, it will do the same thing as if you had just written `2;` by itself. That is to say, it will do nothing. – François Andrieux Oct 10 '17 at 15:12
  • 1
    In this context `a=1,2,3;` is equivalent to `a=1; 2; 3;`. `2;` and `3;` have no effect. – rustyx Oct 10 '17 at 15:13
  • The expressions in the place where you have 2 and 3 will get evaluated, whihc might have side effects. – Yunnosch Oct 10 '17 at 15:15

4 Answers4

8

This:

a = 1,2,3;

is equivalent to:

a = 1;
2;
3;

Thus, 2 and 3 are going to be evaluated, but have no effect.


You could compile with g++ main.cpp -Wall -Wextra and let the compiler warn you:

warning: right operand of comma operator has no effect [-Wunused-value]
     a = 1,2,3;
             ^
warning: right operand of comma operator has no effect [-Wunused-value]
     a = 1,2,3;
              ^
gsamaras
  • 69,751
  • 39
  • 173
  • 279
  • 1
    Oh! I didn't type the `Wall` flag to see the warnings which are in themselves an answer to my question! Now I am 100% clear. Thanks!! – Miraj50 Oct 10 '17 at 15:21
  • @Miraj well you already knew what I didn't know some time: [What does i = (i, ++i, 1) + 1; do?](https://stackoverflow.com/questions/30614396/what-does-i-i-i-1-1-do), so I admire you, glad I helped! – gsamaras Oct 10 '17 at 15:36
  • 1
    :D , I know such comments(as this) is not appreciated, but I `love` this community!! – Miraj50 Oct 10 '17 at 15:39
3

a = 1,2,3 is an expression equal to 3 with the side effect of assigning 1 to a.

That is, you could write

int b = (a = 1, 2, 3);

and b would have the value 3, and a 1.

The evaluation of 2 never has an effect. Do you have your compiler warnings turned down?

Bathsheba
  • 227,678
  • 33
  • 352
  • 470
0

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

So in general this expression statement

a = 1,2,3;

looks like

( a = 1 ), ( 2 ), ( 3 );

(more precisely like ( ( a = 1 ), ( 2 ) ), ( 3 ) that is the expression contains two comma operators)

As the results of evaluation of the expressions ( 2 ) and ( 3 ) are not used the compiler can remove them from the generated object code. Only the expression ( a = 1) has a side effect of changing the variable a.

So the compiler can consider this statement just like

a = 1;

On the other hand if to consider the declaration

int a = ( 1,2,3 );

then here the initializer is an expression with the (two) comma operator. So the declaration is equivalent to

int a = ( ( 1 ), ( 2 ), ( 3 ) );

As the expressions ( 1 ) and ( 2 ) are evaluated as void expressions and do not have side effects the compiler can remove them from the generated object code and consider the declaration like

int a = 3;
Vlad from Moscow
  • 265,791
  • 20
  • 170
  • 303
0

When you do

#include<iostream>
int main()
{
    int a;
    a=1,2,3;
}

Full compiler log The compiler will just do

     mov dword ptr [rbp - 4], 1 

will ignore 2 and 3 and warning as mentioned by gsamaras

Hariom Singh
  • 3,308
  • 5
  • 26
  • 50