Hi folks,

I've simple question regarding following code.

int main()
{
	int i =5;
	printf("%d %d %d\n",i,i++,++i);	// 7, 6, 7
	return 0;
}

The answer is 7 6 7.

I want to know how it is coming.

Please help.


Thanks,
Amar

Recommended Answers

All 20 Replies

This is because ,in this case , printf() is right justified i.e. it works from right to left.

Dear DJSAN10,

Thanks for the reply.
But if printf is right justified then how it is getting 7 for 3rd i (i.e ++i).
It is should get 6 for i's original value is 5.

Thanks,
Amar

Even I was thinking on the same. Did you execute the program. I will try and find out the answer for this

No, it's because of undefined behavior. See this

commented: hey thnx.. +3

Hi WaltP,

If it is because of undefined behavior then how every time answer is coming as 7 6 7.
Any idea? Do you have any explanation for this.

Thanks,
Amar

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).

It is not guaranteed that an increment or decrement is performed immediately after giving up the previous value and before any other part of the expression is evaluated. It is merely guaranteed that the update will be performed sometime before the expression is considered "finished".

So, using of increments or decrements 1 at a time gives accurate result. But if used collectively, the compiler itself chooses the increment or decrement to be performed and gives you a messy result. :P

does that means we can never calculate the answer?

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.

well,if we get some questions of this type in exams or so then no one could deduct our marks?haha

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.

Well it is undefined in gcc compiler...but in Dev C++ or Turbo C++, the compiler works on the operation Right to left.
As you have already given the value of i = 10...so the compiler will first operate it by ++i and displays 6...then it will operate i++ and gives 6 again (as i is printed before the increment is applied)..and at last the i is printed (which has become 7 coz of previous i++ operation)...So the output will be 6 6 7..
I dont know about gcc compilers in linux...haven't tried yet on them..

So, if you got the question in exam...then you must answer them on the basis of the type of compilers they use to teach you.

Well it is undefined in gcc compiler.

It's undefined in the C 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.

Okay, I was just telling him that if his teacher is teaching him on same compiler that i use, then the answer is mentioned above.

you should get 7 6 6
and its simply bcoz printf has the precedence from Right to Left.

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?

Did you not read the thread before posting your incorrect answer?

I compiled it on Dev C++ and that is exactly the output i got. Please tell me what is wrong about it?

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.

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.

Thanks O! Great One.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.