0

It is related to C programming language. Can anybody explain side effects and sequence points in easy language?

And why the pre-increment operator is well defined in C language (what are the reasons behind it) why not the post? Example : int main() { int a=10; printf("%d %d %d\n", a++, a++, a++) ; int b = 10; printf("%d %d %d", ++b, ++b, ++b) ; return 0;} Output: 12 11 10 13 13 13 Acc. To C standards, the order of evaluation of the arguments in a function call is unspecified. So the order in which the three a++ evaluated is not guaranteed. But how the value for ++b is evaluating ? //The behavior is well defined for the pre-increment operator// okay. When the same program runs using volatile keyword then the value of ++b gets evaluated as 13 12 11.. I am so much confused here between volatile and without volatile.

22nd Feb 2023, 6:09 AM
Nikhil Chauhan
Nikhil Chauhan - avatar
2 Antworten
+ 6
You don't even need to drag volatile into this to get more confusion. Just try to run the same snippet on various C compiler, then you'll see that your statement "behaviour of pre-increment operator is well defined" might need a revision cause the output may vary. The subjects you asked for (side effect and sequence point) is rather a difficult subject to explain, I wouldn't dare myself to even try. Wiki has some info on those subjects ... https://en.m.wikipedia.org/wiki/Side_effect_(computer_science) https://en.m.wikipedia.org/wiki/Sequence_point https://en.m.wikibooks.org/wiki/C_Programming/Side_effects_and_sequence_points I don't know whether they wrote it in easy language for the readers, but it might make a good intro.
22nd Feb 2023, 7:28 AM
Ipang