- int i=1,2,3;
- int i=(1,2,3);
- int i; i=1,2,3;
What is the difference between these statements? I can't get to any particular reason for it.
int i=1,2,3;
int i=(1,2,3);
int i; i=1,2,3;
What is the difference between these statements? I can't get to any particular reason for it.
 
    
     
    
    Statement 1 Result : Compile error.
'=' operator has higher precedence than ',' operator. comma act as a separator here. the compiler creates an integer variable 'i' and initializes it with '1'. The compiler fails to create integer variable '2' as '2' is not a valid indentifer.
Statement 2 Result: i=3
'()' operator has higher precedence than '='. So , firstly, bracket operator is evaluated. '()' operator is operated from left to right. but it is always the result of last that gets assigned.
Statement 3: Result: i=1
'=' operator has higher precedence than ',' operator. so 'i' gets initialized by '1'. '2' and '3' are just constant expression. so have no effect .
 
    
    It is the comma operator
i = a, b, c;            // stores a into i      ... a=5, b=2, c=3, i=5
i = (a, b, c);          // stores c into i      ... a=5, b=2, c=3, i=3
the differing behavior between the first and second lines is due to the comma operator having lower precedence than assignment.
