Comma operator in CLI/C++

OK so i have been reading this book and in the "Comma operator " section (Part of the cli/c++ basics) i stumbled into this excersise:

[code]
int a = 2;
int b = 3;
int c = (b++,a= b++ * a++,a%b);

I first tried to understand this on paper (after reading the entire section of course) and i thought that what happes in the 3rd line of code is this:
1)the value of b is incremented by 1 (4)
2)the value of a is set to be equal to yet again the incremented value of b(5) multiplied with the incremented value of a (3)[a = 5 * 3 = 15]
3) the value of c is in the end assigned the value of modulus b--Thats the part which i don't get --

In the described case above i thought the c would get the value of 0 as 15/5 leaves a remainer of 0, but obviously i am wrong.I compiled the programm just to find the mistake i made in paper(obviously with adding the needed structural parts[namespaces,funtions]) and i added Console::WriteLine("{0} {1} {2}", a,b,c) in the end of the code just to see the values of these variables after the code executes.
The results i got is 9 5 4.
Could someone explain to me where my logic is wrong ?

Last edited on
closed account (48T7M4Gy)
https://en.m.wikipedia.org/wiki/Comma_operator

Writeline is not c++

it is probably my bad(as the beginner i am ) for confusing c++/cli with standard c++.I actually managed to found a solution to my problem .

(For people who may run into a similar problem and still are silly enough to make a mess in their head with c++ and c++/cli ):

This had to do with lvalues/ravlues.In this situation the a=b++ * a++ was actually a=4 * 2 = 8 because the a is incremended afterwards.Thats where things got messed up.
[.]
b++ // b becomes 4
a= 4 * 2 = 8// a isn't incremended yet
a= 9 //now it gets incremended

9%5 = 4 // Now it makes sense
Topic archived. No new replies allowed.