Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
different answer in different compiler

in turbo c++ output is 21 and in any other output is 19.

C++
#include<stdio.h>
void main()
{
int i=4,x;
x= ++i + ++i + ++i;
printf("%d",x);
} 

actual output is 19 how comes please explain it

What I have tried:

in turbo c++ output is 21 and in any other output is 19.

actual output is 19 how comes please explain it
Posted
Updated 17-Jun-19 10:05am
v2
Comments
[no name] 17-Jun-19 13:04pm    

The behavior of the pre- and post-fix operators in C are not exactly well-defined. The implementation of those operators varies from compiler to compiler.
 
Share this answer
 
Quote:
actual output is 19 how comes please explain it

Simple, you are in the gray zone, the C compiler is free to rewrite your code the it sees fit.
Any formula with multiple increment/decrement on same variable in unpredictable, the result can also change with versions of same compiler or can depend on optimizations options.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900