C, standard says which operand belongs to which operator. It does not says
which one gets evaluated first.
Why it is returning 8 is because of sequence point. It tells that, all the
side effects of a value are resolved before an operation is performed.
In your case ++i + ++i evaluates like this on gcc 4.2
++2 which is 3
++3 which is 4
now sequence point is resolved both i + i is added to yield 8.
You should not use such expression
More information can be found at
http://c-faq.com/expr/seqpoints.html
http://en.wikipedia.org/wiki/Sequence_point
-
Azhar.
On Mon, Feb 13, 2012 at 7:38 PM, Ratan <[email protected]> wrote:
> Actually i had evaluated x=(++i + ++i); separately with i=2;
> but i was amazed to get answer as 8... can u elaborate it
>
> On Mon, Feb 13, 2012 at 4:09 PM, Rahul <[email protected]> wrote:
> > Here you go then :
> > ++ i becomes 3
> > second bracket becomes 5 ( 3+2)
> >
> >
> > first bracket is 4
> >
> > 4 * 5 = 20
> > But you are adding ONE to i FOUR times
> > hence the answer you get is 4 plus the computation of previous line
> >
> > Hope I AM correct :
> >
> >
> >
> >
> > On Mon, Feb 13, 2012 at 3:56 PM, Ratan <[email protected]>
> wrote:
> >>
> >> can any1 explain the working of the above expression in any of the
> >> compiler..... I jzz want to knw how the computation takes place in gcc
> >> compiler..
> >>
> >> On Sun, Feb 12, 2012 at 11:03 PM, Kartik Sachan <
> [email protected]>
> >> wrote:
> >> > its a compiler depentent...
> >> >
> >> > --
> >> > You received this message because you are subscribed to the Google
> >> > Groups
> >> > "Algorithm Geeks" group.
> >> > To post to this group, send email to [email protected].
> >> > To unsubscribe from this group, send email to
> >> > [email protected].
> >> > For more options, visit this group at
> >> > http://groups.google.com/group/algogeeks?hl=en.
> >>
> >>
> >>
> >> --
> >> --
> >> Ratan | 3rd Year | Information Technology | NIT ALLAHABAD
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups
> >> "Algorithm Geeks" group.
> >> To post to this group, send email to [email protected].
> >> To unsubscribe from this group, send email to
> >> [email protected].
> >> For more options, visit this group at
> >> http://groups.google.com/group/algogeeks?hl=en.
> >>
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> > http://groups.google.com/group/algogeeks?hl=en.
>
>
>
> --
> --
> Ratan | 3rd Year | Information Technology | NIT ALLAHABAD
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.