"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 Rumi
well actually its simple.
initially a=1
b=++a + a++ + a++ + a++ ---->in this expression u first use a pre-increment
operator. so a becomes 2.
now the calculation becomes b=2 + 2 + 2 +2 =8.
the a++ in the remaining part is a post-increment operator.it is only carried
out after the expression is calc.
hope u understood
---------------------------------
Heres a new way to find what you're looking for - Yahoo! Answers