why increment Operator Producing such an output

#include<constream.h>
void main()
{
clrscr();
int n=10 ;
int b ;

// 13 13 13
b = ++n + ++n + ++n;
cout<<"\n B = " <<b; //39

n =10;
// 11 12 13
int h = ++n + ++n + ++n;
cout<<"\n H = " <<h; //36

getch();
}
Last edited on
b = ++n + ++n + ++n;

This does three increments, and three additions. The order in which those happens is undefined.

Could be:

1
2
3
4
n=n+1;
n=n+1;
n=n+1;
b = n + n + n;

Doesn't have to be. Could be something else. Could be all mixed up. You're doing something that the C++ programming language doesn't specify.


The C++ programming language doesn't say what the "right" order is. There is no "right" order.

Don't do it. Don't do it. Don't do it.
Last edited on
Topic archived. No new replies allowed.