+ 5

Please explain me.... How the answer came?

a=1; printf("%d %d %d", a, ++a, a++) ; Answer is : 3 3 1 Please explain how it came....

20th Nov 2019, 1:54 AM
bossCoder
bossCoder - avatar
6 Respostas
+ 7
There is nothing to explain. Your output is not the definitive one, output relies solely on compiler implementation. That is, you can test and observe such expression in different compiler, and you will get different output between compilers. Tips: - Avoid any attempt to mix accessing and modifying a data within a single execution sequence. The standards does not specify how compiler should do this sequentially, you can expect different results from different compiler. - https://en.m.wikipedia.org/wiki/Sequence_point
20th Nov 2019, 5:11 AM
Ipang
+ 5
Just in this case the values are added from right to left but in general there is no single order of evaluation by the compiler for such statements, it might give you different results at times. The order is random. In your case a++ is done first then ++a and then a. The order of printing the values varies is all I'm saying.
20th Nov 2019, 2:14 AM
Avinesh
Avinesh - avatar
+ 3
Avinesh can u explain?
20th Nov 2019, 2:06 AM
bossCoder
bossCoder - avatar
+ 2
Evaluate from right to left.
20th Nov 2019, 2:05 AM
Avinesh
Avinesh - avatar
20th Nov 2019, 7:09 AM
Bilbo Baggins
Bilbo Baggins - avatar