preincrement operator problem

Whats the output for the code -
void main()
{
int i=4,x;
x=++i + ++i + ++i;
printf("%d",x);
}

Please specify the coreect reason too along with output..
Last edited on
The C++ standard does not specify what will happen. The output of your program will depend on your compiler.
Whats the output for the code
Anything. Literally anything. This code contains undefined behavior and it is allowed to do literally anything. It might format your hard drive and you will nothing to complain about.

Anyone telling otherwise should be smacked in the face by IS printout.
output:
printf("%d",x);
It always provide the same output i.e 21
But the reason i am not understanding..please if anyone knows please reply..
It always provide the same output i.e 21
You happens to get 21.
I get 19. Or 21. Or 20. Or 0. Depending on compiler keys, computer I compile on, compiler and moon phase.

The only correct answer is "undefined". And nobody shold write code like that because it is unpredictable and fragile. (also void main() is illegal too, configure your compiler properly if it still allows that)

Read this to get some info on why is it so:
http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points
what output do are you expecting?
21 but dont know why its soo..
It should give 18 as i expect but not giving..
Another code of similar kind,

int main()
{
int i=4;
int x=++i + ++i + ++i;
cout<<x;
return 0;
}

Its giving output as 18 but not the above code..that is giving 21 as output
why 18?
x = (4-1)+(4-1)+(4-1) is what i see logically.
As I told, it is undefined behavior. Anything could happen. There are no restrictions on value of x, because what you doing is not allowed in strictly conforming C++ code.

You already seen how fragile such constructs: aliitle change in code and they give you different answer. So stop trying to predict what will happen and just do not do that.

Your compiler should warn you about UB here:
warning: operation on 'i' may be undefined [-Wsequence-point]


In your first case compiler happened to applied all side effects before evaluating expression; in second one, it happened to apply them as it evaluated value of corresponding argument.
Thanks for the suggestions..
But its showing same output in all my college workstations
I use borland turbo C++ IDE..
The answer is 18 as
x=(4+1)+(5+1)+(6+1);

Incremented i gets passed to next value of i..
And in first code..the last changed value of i gets adopted in all i's i.e
x=7+7+7;
Therefore,it gives 21
I also do it with
x=++i + ++i;
It gives 12
And with
x=++i + ++i + ++i + ++i;
It gives 32 as output..now what to say..
But its showing same output in all my college workstations
Because they are on same platform, using same (terribly outdated and bug-ridden) compiler with same configuration.

Other compilers/platforms/options coud give other results:
http://ideone.com/zHRm6U
http://coliru.stacked-crooked.com/a/35f6046276e281c2
http://melpon.org/wandbox/permlink/AZM9oRvbOBMg0pqu
OP: You are trying to find logic where there is none. This is bad code and will do unpredictable things.

As has been mentioned, the result of this code is undefined. So "why does it do this" is a question that doesn't make sense.
you can't decrement a variable by another during initialization.. and if you can you shouldn't. that's what im reading.
Can u suggest me wid new edition compilers or IDE where i could make my C better...
First of all I would suggest some C++ books. As answered here: http://www.cplusplus.com/forum/general/155123/

Other than that, I suggest to get (I presume, you are using Windows) either Visual C++2013 or Code::Blocks + MinGW. Read documentations on compiler and turn on warnings as high as possible.
Topic archived. No new replies allowed.