Question

I've known that primary operator (x++) is different form unary operator (++x) when combine with another operator in a statement.

But I wonder whether those two operator is same when leave them alone on the statement. I mean about compiled code, time to run, ... between:

++x;

and

x++;

Which x is an integer variable.

Was it helpful?

Solution

It is the same in disassembly (not IL) on Windows 8 x64:

                ++x;
0000004a  inc         dword ptr [ebp-40h] 
                x++;
0000004d  inc         dword ptr [ebp-40h] 

As you can see, both statements are an inc instruction. I used this code to try out:

int x = 0;
for (int i = 0; i < 10; i++)
{
    ++x;
    x++;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top