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
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?

Replies

  • Sanyam Khurana
    Sanyam Khurana
    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.

You are reading an archived discussion.

Related Posts

I might be late to the party, but just stumbled upon the following wired broadband (FTTH, I guess) plans being offered by Tata Docomo folks. Compared to what we pay...
The electronic mail seems to be the problem that remains unsolved; and IBM's taking a fresh look at the problem with a new email client, called IBM Verse. Email can...
Google's just announced the availability of Google Chrome V. 39.0.2171.65 for OSX - the stable version of Chrome that adds 64-bit support. The latest version, which should have been installed...
Read an interesting story of some guy on Quora, this is all about his journey to get selected in Google The journey: Summer 2013: Started to revise the course Data...
Hi, I am presently working in IT industry having 3 years of experience. I now feel to move to some other country like US or Canada. Since i am working...