# postfix n prefix doubt !!!!

now dis is really getting on my nerves... i'm a c/cpp programmer .. but recently stumbled upon dis basic piece of code n quite stuck wid it !
[code]
int a=1;
a=a++ + ++a;
a=++a + ++a;
a=a++ + a++;
a=++a + a++;
cout<<a;
[/code]

i still am confused on the result i got... it says a is 63. after goin thru the code again i found out dat a=++a + ++a; behaves differently than a=a++ +a++; tho i'm aware of the logic behind the postfix n prefix opereator and its precedence .. i still cant figure out the logic behind the ouput ?
plzzzz help
thanks...
"[b]The[blue] GEEK[/blue] Shall Inherit The Earth" ;-) [/b]

• : now dis is really getting on my nerves... i'm a c/cpp programmer ..
: but recently stumbled upon dis basic piece of code n quite stuck wid
: it !
: [code]:
: int a=1;
: a=a++ + ++a;
: a=++a + ++a;
: a=a++ + a++;
: a=++a + a++;
: cout<<a;
: [/code]:
:
: i still am confused on the result i got... it says a is 63. after
: goin thru the code again i found out dat a=++a + ++a; behaves
: differently than a=a++ +a++; tho i'm aware of the logic behind the
: postfix n prefix opereator and its precedence .. i still cant figure
: out the logic behind the ouput ?
: plzzzz help
: thanks...
: "[b]The[blue] GEEK[/blue] Shall Inherit The Earth" ;-) [/b]
I expanded the statements into simpler ones, and got near the result. I don't know much about the details of predecendence, though.
[code]
int a=1; a=1
a=a++ + ++a; a++=2, a=2, ++a=3, a=3, a=a++ + ++a=2+3=5
a=++a + ++a; ++a=6, a=6, a++=7, a=7, a=++a + a++=6+7=13
a=a++ + a++; a++=14, a=14, ++a=15, a=15, a=a++ + ++a=14+15=29
a=++a + a++; ++a=30, a=30, a++=31, a=31, a=++a + a++=30+31=61
cout<<a; output 61
[/code]
• : : [code]: :
: : int a=1;
: : a=a++ + ++a;
: : a=++a + ++a;
: : a=a++ + a++;
: : a=++a + a++;
: : cout<<a;
: : [/code]: :
: :

The C compiler reads this statement from [b]Right to Left[/b]. With ++ prefix notation, the value of a is incrementen and then passed to the + operator. With postfix, it's first passed to the + operator and then incremented (meaning the + operator has the non-incremented value on the left). Then at the left side of the + operator, the changed value is used and again the prefix/postfix rules apply.
I haven't tested it, but this is how it should work.

Just for the simple fact that it requires this much text to describe, it's bad programming habbit to write code like this. You should always use the longer form to make clear what you mean - because often you won't be writing what you mean if you do it like this.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
• now here's de catch ...! the value of a is 14 after the 3rd line of the code [:(] ... dats were i'm stuck... n still cant figure out how . yes i'm aware of the execution of the statement from left to ryt . n this code mite have some kinda trick in it ...

"[b]The[blue] GEEK[/blue] Shall Inherit The Earth" ;-) [/b]
• It's actually far more logical than I thought. cactus1 got it almost right:
[code]
int a=1;
a=a++ + ++a;
a=++a + ++a;
a=a++ + a++;
a=++a + a++;
cout<<a;
[/code]
Equals:
[code]
int a = 1;
a++; a = a + a; a++; //First a++ is prefix, last is postfix
a++; a++; a = a + a;
a = a + a; a++; a++;
a++; a = a + a; a++;
[/code]
So, all prefixes are done before the statement. Then it is calculated normally, ignoring any prefixes and postfixes. Then after that, the postfixes are applied.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
• : It's actually far more logical than I thought. cactus1 got it almost
: right:
: [code]:
: int a=1;
: a=a++ + ++a;
: a=++a + ++a;
: a=a++ + a++;
: a=++a + a++;
: cout<<a;
: [/code]:
: Equals:
: [code]:
: int a = 1;
: a++; a = a + a; a++; //First a++ is prefix, last is postfix
: a++; a++; a = a + a;
: a = a + a; a++; a++;
: a++; a = a + a; a++;
: [/code]:
: So, all prefixes are done before the statement. Then it is
: calculated normally, ignoring any prefixes and postfixes. Then after
: that, the postfixes are applied.
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry

but mr.richard as far as i knw both postfix and prefix operators have higher precedence over the '+' addition operator ... so according to dat the postfixes must b applied first ..later comes the prefix and the addition operator at the end . aint it ??
"[b]The[blue] GEEK[/blue] Shall Inherit The Earth" ;-) [/b]
• :
: but mr.richard as far as i knw both postfix and prefix operators
: have higher precedence over the '+' addition operator ... so
: according to dat the postfixes must b applied first ..later comes
: the prefix and the addition operator at the end . aint it ??
: "[b]The[blue] GEEK[/blue] Shall Inherit The Earth" ;-) [/b]

By the simple fact that this example I gave IS the way it is calculated: no.

Prefix incrementation increments before the statement.
Postfix after the statement.

It could have ofcourse been different, but the ANSI C standard picked this way. This way is least fuziest.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry