CrazyEngineers
  • Correct Output Of: y = x++ + ++x [In C & C++] | Concept Of Sequence Points

    Kaustubh Katdare

    Kaustubh Katdare

    @thebigk
    Updated: Oct 27, 2024
    Views: 1.4K
    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
    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
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Sanyam Khurana

    MemberNov 18, 2014

    These type of codes would give different output depending on different compilers. But the result would remain un-defined.

    Un-fortunately, many teachers that teaches C as a subject don't know about sequence points and moreover, make students use Turbo C compiler. 😕

    I've myself gone through this trauma both in school as well as college.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register