mardi 24 mars 2015

Wrong understanding of compilers executing ++ operator


Consider the following code (although I have tagged C# and Java, this ain't for C# or Java specifically....doesn't make a difference though for this should apply for any language!):



int x = 45;
x = x + x++;


Now the way I understand it, when the compiler reads the above code, it queues the execution of the ++ operator and executes the remaining first.


So the outer statement gets executed (which makes x as 90), then the ++ operator gets executed.


So that should make x as 91 right?


However, when I execute this code, x a the value of 90. I have no clue how this happens. I need some step-by-step explanation as to how the value remains as 90.


Speaking of which, how exactly do compilers work? I don't mean code wise, but theory wise....how do they parse an expression and execute statements? (I know there are loads of books on this topic but I am talking about the hierarchy in which they execute statements. Which do they execute first? Which next?)





Aucun commentaire:

Enregistrer un commentaire