No, it's because of undefined behavior. See this
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
If it is because of undefined behavior then how every time answer is coming as 7 6 7.
Undefined is not synonymous with inconsistent. On the same compiler, undefined results could very well be the same for every run. But there's no guarantee of that, or that other compilers or even other versions of the same compiler will have the same result. Undefined means there are no guarantees for the duration of the program (ie. if you invoke undefined behavior, your entire program becomes undefined henceforth).
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
does that means we can never calculate the answer?
Not until the undefined behavior is fixed. Amazingly enough, fixing the undefined behavior also gives you control over evaluation of the answer. The reason it's undefined is because the compiler is free to evaluate the expression in multiple ways and all of those ways are equally good.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
well,if we get some questions of this type in exams or so then no one could deduct our marks?haha
If you get questions of that type in exams, you should complain to the instructor because the answer is subjective. Too many tests will post questions like this where the answer depends not on what actually happens, but what the test creator believes will (or should) happen.
But be prepared to prove that the question is erroneous, because almost invariably you'll be marked down for not answering it. Instructors who don't know C well enough to teach it are likely to be stubborn in their ignorance.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Well it is undefined in gcc compiler.
It's undefined in theC language specification. Compilers can do anything they damn well please when it comes to undefined behavior.
...but in Dev C++ or Turbo C++
It's still undefined. Just because you can pick out the method used in a specific compiler doesn't make the code any less broken.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
you should get 7 6 6
and its simply bcoz printf has the precedence from Right to Left.
Did you not read the thread before posting your incorrect answer?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I compiled it on Dev C++ and that is exactly the output i got. Please tell me what is wrong about it?
It doesn't matter what result your compiler gives you. Allow me to summarize since you're reading-impaired (ie. the following has been said multiple times already in this thread):The behavior is undefined, and thus, completely unpredictable. Your mistake is assuming that test results from Dev-C++ will be consistent on all other compilers, which is wrong.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401