4
  1. int i=1,2,3;

  2. int i=(1,2,3);

  3. int i; i=1,2,3;

What is the difference between these statements? I can't get to any particular reason for it.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Robert Roman
  • 43
  • 1
  • 1
  • 6
  • Please improve your title. A future visitor with the same question will not find this question because the title conveys no indication what it is asking about. (Answer: Operator precedence.) – Raymond Chen Jun 22 '13 at 14:08
  • Raymond Chen - This is what i thought would be best for it. i would be thankful if you can edit please ! – Robert Roman Jun 22 '13 at 14:09
  • 3
    You honestly thought "What is the difference between two statements" is a good title? They could be any two statements! – Raymond Chen Jun 22 '13 at 14:11
  • It is very close to a duplicate of 'Please explain comma operator in this program', but the commas in the variable definition do distinguish this question from that. – Jonathan Leffler Jun 22 '13 at 14:13
  • @Raymond Chen- This is not duplicate.both are differenet ! – Robert Roman Jun 22 '13 at 14:14
  • 1
    @RaymondChen: Robert is new to the site and may not even know what those statements do. Rather than berate someone, perhaps try to be more constructive and actually edit the question to provide a more descriptive title. – lnafziger Jun 22 '13 at 14:14
  • 2
    @JonathanLeffler Then try [What does 'int x = (anyInt1, anyInt2);' mean?](http://stackoverflow.com/questions/13209399/c-what-does-int-x-anyint1-anyint2-mean). – Raymond Chen Jun 22 '13 at 14:14
  • @Konrad Rudolph - This is not duplicate.Please look carefully ! – Robert Roman Jun 22 '13 at 14:18
  • @Robert The *question* may not be an exact duplicate but the accepted answer in the other question gives the relevant explanation. – Konrad Rudolph Jun 22 '13 at 14:22
  • that means it's not duplicate ! –  Jun 22 '13 at 14:23
  • 1
    a better duplicate would be http://stackoverflow.com/q/20163411/995714 – phuclv Jul 30 '15 at 05:18

2 Answers2

16
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 .

S J
  • 3,210
  • 1
  • 22
  • 32
0

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.

Jayram
  • 18,820
  • 6
  • 51
  • 68