Correct Output Of: y = x++ + ++x [In C & C++] | Concept Of Sequence Points
So, I was reading that most of the college teachers love to ask this question in the exams:
If x=1 and y = 2, then what will be the output of
The expression is against the laws of C programming language that a variable can be changed not more than once before a new sequence point is met.
Would love to see the concept of sequence points discussed in depth here. Can we have the code executed in different compilers so that we can see & analyse the results? What say?
If x=1 and y = 2, then what will be the output of
y = x++ + ++x + y++ + ++y;It appears that the correct answer to the question is that the output is undefined! Why? Because you are trying to manipulated the same variable before the compiler meets the 'Sequence Point'. A Sequence Point is a point at which the compiler has to confirm that it's done all the previous operation. In general, the order of the calculations does not matter in most of the situations; but when you are trying to manipulate one variable twice the result is undefined!
The expression is against the laws of C programming language that a variable can be changed not more than once before a new sequence point is met.
Would love to see the concept of sequence points discussed in depth here. Can we have the code executed in different compilers so that we can see & analyse the results? What say?
0