Solved

Simple question about ++ and --

Posted on 2002-05-07
12
177 Views
Last Modified: 2010-04-02
The question is

what is the answer of the following code

int x,n=5;
x=++n*--n
cout << x;


the problem is I solved this code manually and got 30
but if I use VC++, it said 25!

Why? my friend said they use c on AX machine, the answer is 30.

please tell me about this...
I am confusing
0
Comment
Question by:StingRaY
12 Comments
 
LVL 16

Accepted Solution

by:
AlexNek earned 50 total points
ID: 6994649
2 StingRaY
AFIAK: ++ and -- has highest priority as *.
1. n = n+1  // n = 6
2. n = n-1  // n = 5
3. x = n*n  // x = 5*5 = 25
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 6994664
An expression like
++n*--n is bound to have side effects. The actual interpretation will vary from compiler to compiler. So you are getting different results.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6994705
AlexNek,
The last line in your answer is incorrect.
It should equal to 30.
If VC++ is comming up with 25, then it's a compiler bug.

3. x = n*n  // x = 6*5 = 30
0
 
LVL 30

Expert Comment

by:Axter
ID: 6994712
To prove it's a compiler bug, run the following code:

int SendNumBack(int n)
{
     return n;
}
     
int main(int argc, char* argv[])
{
     int x,n=5, n2=5, n3=6;
     x = (++n) * (--n);
     std::cout << "x = " << x << std::endl;
     
     x = SendNumBack(++n) * SendNumBack(--n);
     std::cout << "x = "  << x << std::endl;
     
     system("pause");
     return 0;
}

If the compiler is working properly,  both values will be the same.
You'll notice that VC++ returns two different values.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 6995137
2 Axter
I'm sorry but I not agree with you. It is not a compiler error.
The first sample in your code working as I described above, the second use temporary storage.

1. Register X1 = n+1  // n = 6
2. Register X2 = n-1  // n = 5
3. x = Register X1*Register X2  // x = 6*5 = 30

0
 
LVL 30

Expert Comment

by:Axter
ID: 6995159
AlexNek,
What do you think the first example should produce?
x = (++n) * (--n);

That should produce 30, but in VC++, the output is 25.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Axter
ID: 6995167
AlexNek,
>>The first sample in your code working as I described
>>above, the second use temporary storage.

Both examples should produce the same value in a correctly working compiler.

The first example should equal to the following:
x = (++n) * (--n);
1. (++n) = n+1  // n = 6
2. (--n) = n-1  // n = 5
3. x = (++n)*(--n); // x = 6*5 = 30

And as you have already posted, the second example should also result in a 30.

However, in VC++, the first output is 25, and the second is 30.

This is a bug in the compiler.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 6995173
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 6995188
2 Axter
show me compiler error here, please.

10:   int x,n=5;
   mov         [n],5
11:   x=++n*--n;
   mov         eax,[n]
   add         eax,1
   mov         [n],eax
   mov         ecx, [n]
   sub         ecx,1
   mov         [n],ecx
   mov         edx,[n]
   imul        edx,[n]
   mov         [x],edx
0
 
LVL 30

Expert Comment

by:Axter
ID: 6995268
AlexNek,
Since it's been decades since I last programmed in assembly language, the above comment has little meaning. :-)

The link you posted seems to support your view point, but it seems to be talking about what order the evaluation should take place in.

1. (++n) = n+1  // n = 6
2. (--n) = n-1  // n = 5
3. x = (++n)*(--n); // x = 6*5 = 30

OR

1. (--n) = n-1  // n = 4
2. (++n) = n+1  // n = 5
3. x = (++n)*(--n); // x = 5*4 = 20

No matter which order it goes in, the value still would not be 25.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 6995288
2 Axter
>The link you posted seems to support your view point,
give me link which support your view point, please.

Why you think that left operand in N3 still 5 or 4?

x = current value of N * current value of N
and current value of N in both cases = 5
0
 
LVL 15

Author Comment

by:StingRaY
ID: 6996798
Thanks AlexNek and Axter.

I am wondering that

when an expression ++n*--n is performed,
where n is stored in memory.

the problem is where should the result of "++n" be kept when ++n is perform.

if result of ++n is stored in the same location of n
n should be updated to 6.

and the same reason --n will change that value to 5

after that VC++ will use that value for multiplication.

I don't know is it correct or not.


please describe.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now