+ 1

hello. please help me to understand this code: int main(){ int i=50, b=20,c=30; i=c,b; cout<<i<<endl; i=(c,b); cout<<i<<endl; } outputs: 30 20 I don't realize, what does the parantheses change (3 and 5 line)

23rd Jun 2016, 4:42 PM
Dennis
Dennis - avatar
3 Réponses
+ 1
it's related to , operator. when u assign any value to variable with , operator it will take 1st value but the parentheses have more precedence than coma so complier read () from left to right and assign last value to variable
23rd Jun 2016, 7:59 PM
Zuber Ahmed
Zuber Ahmed - avatar
0
Not bad code -_- How I know, if there are parantheses, variable takes last element in parantheses, else it takes first element. I haven't see such code anywhere, but I think it is caused by compiler(It wants to read to final in parantheses).
23rd Jun 2016, 5:50 PM
Dmitry Pudov
Dmitry Pudov - avatar
0
Ehm pls never ever write code like this. I've never seen this in my entire life and I have seen bad code... Why is it bad? It does not fulfill a purpose that cannot be expressed in an easier, much more readable way.
23rd Jun 2016, 7:24 PM
Stefan
Stefan - avatar