471,873 Members | 2,037 Online

# Complex Expression

Dear thescripts
I am Anwar, please explain this expression how it becomes 24

{
int a = 10 ;
a = ++a + ++a;
cout << a ;
}

this program shows 24 on the screen so i can't understand this expression please explain it how it becomes 24
Oct 10 '06 #1
10 3927
Preincrement ++ has higher precedence than +. That's why in your expression
Expand|Select|Wrap|Line Numbers
1. a= ++a + ++a
all the ++ operations are performed first, making a = 12, after that a = a + a = 24;
Oct 10 '06 #2
its a thing of memory,..
when ++a is encountered, the value in address of a is changed.. so when in second ++a, the same address is changed as 12..

so when addition is operated, 12+12 is evaluated..

if wrong pls tell me the answer...
Oct 10 '06 #3
Dear thescripts
I am Anwar, please explain this expression how it becomes 24

{
int a = 10 ;
a = ++a + ++a;
cout << a ;
}

this program shows 24 on the screen so i can't understand this expression please explain it how it becomes 24
If we write it out as a changes we'd see this:
a = 10
++a + ++a

a = 11
a + ++a

a = 12
a + a which equals 24.

but if you wrote it as
a = ++(a + a)

you'd get 21 { ++( 10 + 10 ) }

- Miles
Oct 10 '06 #4
this concepts which are given in the reply are for turboc and microsoft compiler
but for gcc compilerits different
int i=5,y=0;
y=(++i)+(i++)+(i++)+(++i);
printf("i=%d\ny=%d",i,y);

now microsoft compiler gives out put
i=9
y=28
and gcc compiler gives out put
i=9
y=25

so tell which one is correct in terms of standardization
and logically which one is best
also tell which is the best compiler for c
Jun 15 '07 #5
gpraghuram
1,275 Expert 1GB
this concepts which are given in the reply are for turboc and microsoft compiler
but for gcc compilerits different
int i=5,y=0;
y=(++i)+(i++)+(i++)+(++i);
printf("i=%d\ny=%d",i,y);

now microsoft compiler gives out put
i=9
y=28
and gcc compiler gives out put
i=9
y=25

so tell which one is correct in terms of standardization
and logically which one is best
also tell which is the best compiler for c
HI,
These type of expressions depends on the compiler u use (That is purely compiler dependent).
So better avoid these type of complex expressions as you cant be sure of the answer.

Raghuram
Jun 15 '07 #6
DeMan
1,806 1GB
better still, always use brackets, even when you think something is implicit - they are very difficult mistakes to find when things start to go wrong.....
Jun 15 '07 #7
ilikepython
844 Expert 512MB
Dear thescripts
I am Anwar, please explain this expression how it becomes 24

{
int a = 10 ;
a = ++a + ++a;
cout << a ;
}

this program shows 24 on the screen so i can't understand this expression please explain it how it becomes 24
Yep, these expressions are undefined and shouldn't be depended on. There have been several threads on this already. You might want to search around the C++ forum for them.
Jun 15 '07 #8
Yep, these expressions are undefined and shouldn't be depended on. There have been several threads on this already. You might want to search around the C++ forum for them.
can any one have any link which tells what ansi c have to say about it
because these expressions are aolved in 3 manners only
1)the turboc3 way
2)gcc way
3) let us c way

1)turboc and mcrosoft
y=(++i)+(i++)+(i++)+(++i);
first new value is calculated
there are 2 (++i) so the value of i is increased at the position and then new value of i becomes 7 now the new expression becomes
y=7+7+7+7;
y=28;

2)gcc
left to right
y=6+6+6+7;
when ++i is observed the value is ioncreased then n ther and is fed in calculation
for i++ its increased after ';' operator
so we get
y=25;

3) let us c way
right to left way
but i++ value also increases after one operator
y=9+7+6+6;
y=28;

this is a bit complex then other
y=(++i)+(i++)+(i++)+(++i);
left<-----------------------right
when ++i is observed value is increased then n there
when i++ is observed value is increased after next operator what ever the next operator may be

now i want to know which one is the standard one and is used by c99 or ansi c because it creates ambiguous situations
Jun 16 '07 #9
JosAH
11,448 Expert 8TB
All the compilers do it the correct way; according to the Standard (both C89 and C99)
define expressions like the one in this thread as "undefined behaviour". If an lvalue
is changed more than once before a sequence point is reached the behaviour
is undefined.

A sequence point is a point in execution where nothing needs to be evaluated
for that moment; a semicolon is a sequence point.

kind regards,

Jos
Jun 16 '07 #10
DeMan
1,806 1GB
by splitting the equation into several steps (or by using relative addition), you can avoid the ambiguity and ensure you always get the result you are looking for
Jun 16 '07 #11