Posted by: Mohamed | 13/05/2012

What is the value of i?

Hello everyone,

I know its been a long time since my last post, but I’m having a lot of stuff at hand now, hopefully once I finish them I will get back to regular writing.

However, I’ve stumbled upon an article asking the same question.

The question is: What is the value of i: int i = 1; i += ++i + ++i;

And I figured out that it is 6. And the writer said that the answer depends on the language you are using you gonna get either 3 or 6 and in the C family you gonna get undetermined result!. So I started my beloved VS and in a fresh C++ project I typed the code, and surprisingly the result was 9 (I believe I’ve got a result because I’m using VS compiler, when I tried the same code on http://www.compilr.com I’ve got a compile error telling me something about undetermined i assignment, or something like that…)

So in order to debug the code, I’ve divided the right hand side and the left hand side and got the following code:

int i = 1;
int j = ++i + ++i;
i += j;

So when I debugged the code, it actually made sense. When you increment i the first time you get i = 2, then when you increment i again you get i = 3, and 3 + 3 = 6, while i still equals to 3 at the left hand side, 3 + 6 = 9.

Now, I was quite curious to see how C# gonna handle this problem, so I launched and new C# project and written the first code : int i = 1; i += ++i + ++i;

And guess what? I’ve got 6

So I’ve tried to do the same trick with C++ and separate the left hand side and the right hand side using the exact same code.

And the result was 8
So I figured out that what C# does the following for the line i += ++i + ++i;  is 1 += 2 + 3;

I think that C# increments the first time and i keeps the number at that position all the time and the same thing for the second increment, and as a consequence you get 6. Somehow i doesn’t get the final evaluation until the compiler moves to the next line. Unlike C++ where the value of i is changed on the fly without waiting for the compiler to move to the next line.

I’ve tried Java btw and I’ve got 6 as well.

What is confusing me here, is that 2 different programming languages give 2 different results for the same problem… it is like one compiler tells you that 2 + 2 =4 and the other 2 + 2 = 5 (or probably I’m exaggerating).

I don’t know what is the logic behind such behavior, probably it is because the language features that could manifest themselves in awkward ways at certain problems…. anyways.. if someone reading this and have a nice scientific explanation of  the logic behind what is happening, it would be great to post it in the comments section ;).

I hope that you enjoyed reading this post ;).

Best regards
Mohamed

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Categories

%d bloggers like this: