Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I tested priority of operators in VS2010 so with this small code
C++
#include <iostream>
using std::cout;
void main(){
	int i=5;
	cout <<"\n i++="<<i++;
	i=5;
	cout <<"\n i++ + i="<<i++ + i;
        i=5;
	cout <<"\n i++ +i++ + ++i="<<i++ +i++ + ++i;
        i=5;
	cout <<"\n i++ + i++ +i + ++i="<<i++ + i++ +i + ++i;
	i=5;
	int b=(i++ +i++ +i);
	cout <<"\n i++ +i++ +i="<<b;
	cout << "\ni++ +i++ +i + ++i="<<b+ ++i;
	i=5;
	cout <<"\n i++ + i++ + i++ + ++i="<<i++ + i++ + i++ + ++i;
	cout<<"\n";
	system ("pause");
}

so answers are really amusing for me
i++=5
i++ + i=10
i++ +i++ + ++i=18
i++ + i++ +i + ++i=24
i++ +i++ +i=15
i++ +i++ +i + ++i=23
i++ + i++ + i++ + ++i=24

and i don't understand how this results are made?
any one can help me?
Posted
Updated 22-Apr-14 6:14am
v3
Comments
PIEBALDconsult 22-Apr-14 12:31pm    
You are relying on undefined functionality.

See also: http://www.codeproject.com/Tips/756158/Unary-Increment-Decrement-Operators-in-C?msg=4799445#xx4799445xx
engrshayeghi 22-Apr-14 13:07pm    
Thank you very much
but i think that i++ + i++ +i + ++i=24 and i++ + i++ + i++ + ++i=24 too, is not acceptable in any way.
and another thing is that, this program compiled in visual studio 2010 without any error or warning.
I'm thinking that we have standard priority tables that are same as in different c++ books. why different compilers do different behavior?
PIEBALDconsult 22-Apr-14 13:16pm    
Because it's undefined . Did you turn on all warnings?
Sergey Alexandrovich Kryukov 22-Apr-14 13:32pm    
VS does not do any calculations here; your code does :-)
Yes, those things with order of operations can be amusing, controversial, etc. What do you want to understand, exactly? Did you try hard to understand them? :-)
—SA
engrshayeghi 22-Apr-14 13:54pm    
sergey you are true
may be we never use a code like i++ + i++ +i + ++i outside of academics but easily codes
like pop-pop; func (pop,pop); or func (i++,++i); .... may be used and i tested and I'm sure
that they behave such a way we don't want.
I only practice to understand which codes are true or not.

It's down to the C++ specification, which is based on the older C specification, which does not state exactly when pre and post increment instructions must be performed - or indeed that the order of execution of instructions must be maintained to be the same as you wrote: the compiler is free to reorder them to suit it's execution pipeline if it wishes.

This means that what you get when you try to play games with pre-and post increment is not defined by the specification and varies from compiler to compiler.

This is not the case in C#, where the execution order is very specifically defined, and the exact timing of pre and post increments are also specified.

C#
i = 10;
x = ++i + 5;

Can be read as:
C#
i = 10;
i = i + 1;
x = i + 5;

Is that it? It's a bit...simple...
Yes, it is. Or, perhaps not.

It is simple, if you use it in simple ways - as an array indexer for example:
C#
x = myArray[index++];
Or as a loop increment:

C#
for (i = 0; i < 10; i++)
   {
   WriteLine(myArray[i]);
   }
But after that, you are into a world of confusion and pain!

For example, what does this leave as a value of i:
C#
int i,j;

i = 10;
for (j = 0; j < 5; j++)
    {
    i = i++;
    }
The answer is: unchanged. i remains at 10. Why? Think of it like this: what does this look like if we expand it?
int i = 10;
i = i++;
If we write this in C#, then the IL looks like this:
.line 14,14 : 13,24 ''
IL_0001:  ldc.i4.s   10                    Push a constant value '10' to the stack, 4 byte integer,
IL_0003:  stloc.0                          Pop the top of the stack into local number 0
.line 15,15 : 13,21 ''
IL_0004:  ldloc.0                          Push local number 0 to the stack
IL_0005:  dup                              Duplicate the top of the stack
IL_0006:  ldc.i4.1                         Push a constant value '1', 4 byte integer
IL_0007:  add                              Pop the top two stack items, add them, and push the result
IL_0008:  stloc.0                          Store the top of the stack in local number 0
IL_0009:  stloc.0                          Store the top of the stack in local number 0
What? In expanded C# code, that comes back as:
C#
int i = 10;
int j = i;
int k = j;
k = k + 1;
i = k;
i = j;
Which is rather strange...because you could throw away the three lines in the middle without affecting the results.

It shouldn't do that, should it?

Yes. Yes, it should: i++ is a postfix operation: It says, "remember the value of i, then increment i by one, and then return the value you remembered".

So what you have told the compiler to do is ignore the result of the increment by overwriting it with the value you started off with.

Interestingly, if you try it in the Visual Studio C++ compiler...it doesn't... because it handles it differently!

So, now we have the first reason why you have to be careful when you start using increment and decrement operators for real: it's effectively a side effect, a whole line of code inserted into your line, and if you don't think very carefully, it won't do what you think.
 
Share this answer
 
Comments
chaau 22-Apr-14 20:11pm    
You just need to add that for C# there is only one compiler: Microsoft. But for C/C++ - dozens
OK This is a not a suitable thing that a programmer think to code behavior in cases like above because converting an algorithm to code can be difficult enough .
for example if we define a stack and to subscribe two higher elements the below code in visual studio is correct
a=pop()-pop()

but this code doesn't what do you want
C++
{
a=sub (pop(),pop())
}
int sub(int a,int b)
{
return a-b;
}

because at first the right pop() (top element in stack) sent as b then next top element (left one in function) sent as a.
but in the above code (wrote by OriginalGriff)
int i,j;
 
i = 10;
for (j = 0; j < 5; j++)
    {
    i = i++;
    }

I think it is equal to
i = 10;
for (j = 0; j < 5; j++)
    {
    i = i;
    i=i+1;
    }

and finally i=15
I hope to see some defined standard about this .
So it seems the best way is to write codes very clear as when we want to learn to 7 years old child.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900