473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 4098
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
if you have any links please provide that too to support your answer
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
if you have any links please provide that too to support your answer
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

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ken in Melbourne Australia | last post by:
If I use the curly bracket syntax (referred to as the complex syntax) within a string, how do I get to call a function within it? The php manual says that the first (or previous) character for...
5
by: Bob Stearns | last post by:
Is there an easy way (without duplication of the complex expression) to use the same complex expression in all three places? Will something like this work? WITH (SELECT t.*, <complex expr> AS...
5
by: Trail Monster | last post by:
Ok, I've been searching the net now for several days and can't find how to do this anywhere. Version: VS 2005 Professional Release, 2.0 Framework Background: I have a complex business object...
1
by: Andrew | last post by:
How can I create a property or function for a typed datasets column? By this I need to add a new column (element) to the table or override an existing column. For instance I have an Invoice table...
6
by: jacob navia | last post by:
Consider this code: < code > #include <complex.h> int main(void) { double complex c = 0.4+2.9I; c = ~c; } < end code >
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.