--- In [email protected], "Brett W. McCoy" <[EMAIL PROTECTED]>
wrote:
>
> On 1/21/07, ankurnot4u <[EMAIL PROTECTED]> wrote:
> > this is some thing intresting !
> >
> > can any one tell why the output of this code is 8 ??
> >
> > {
> > int a=1,b;
> > b = ++a + a++ + a++ + a++;
> > cout<<b;
> > }
>
> You are invoking undefined behavior:
>
> http://c-faq.com/expr/evalorder2.html
>
> -- Brett
> ------------------------------------------------------------
> "In the rhythm of music a secret is hidden;
> If I were to divulge it, it would overturn the world."
> -- Jelaleddin Rum
Since precedence if ++ is more than + so compiler first calculate all
the incrementing values means
for ++a it set the value of a=a+1=1+1=2(prefix increment)
but in case of a++(postfix increment) first it put the value of a in
expression,since the value of a=2 so it use a=2 for all a++.
hencenow expression becomes:
b=a+a+a+a=2+2+2+2=8(The increment in case of a++ will effect after ;)