# evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

• (2 Pages)
• 1
• 2

## 17 Replies - 2000 Views - Last Post: 11 June 2012 - 05:11 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=282358&amp;s=dcb5b088b1910c0be922941138ed0ffe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 11
• Joined: 31-May 12

# evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 12:28 PM

the following is the program which i have come across and tried to solve:
```#include <stdio.h>
void main()
{
int x,y,i=6;
x= i++ + ++i + i++ + ++i * --i + i++ / ++i;
printf("%d\n",i);
printf("%d\n",x);
}
```

my solution is:

i=6 initially.....
the expression is: i ++ + ++ i + i ++ + ++ i * -- i + i ++ / ++ i
i values are: 6 7 8 8 8 9 10 10 9 9 9 10 11 11
so i=11

x=6+8+8+10*9+9/11 == 112.818182 == 112
x=112

is this the correct solution sir????

Is This A Good Question/Topic? 0

## Replies To: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

### #2 ishkabible

• spelling expret

Reputation: 1702
• Posts: 5,861
• Joined: 03-August 09

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 12:30 PM

POPULAR

this is undefined behavior; there is no telling what it will come out to.

Reputation: 0
• Posts: 11
• Joined: 31-May 12

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 12:42 PM

what do you mean sir???

### #4 sepp2k

• D.I.C Lover

Reputation: 2307
• Posts: 3,568
• Joined: 21-June 11

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 12:48 PM

prasad manne, on 10 June 2012 - 09:42 PM, said:

what do you mean sir???

That the behavior is undefined (because it is illegal to modify the same variable more than once without a sequence point in between).

The behavior can (and will) differ when the application is compiled with different compilers or for different platforms or even just with different compiler options. The behavior can even differ when running the same application multiple times. Or when evaluating the same expression multiple times within a single run of the application.

There's also no restriction to how the expression might behave. It could return any number it wants, or it could crash your application or cause an infinite loop or cause nasal demons.

This post has been edited by sepp2k: 10 June 2012 - 01:07 PM

Reputation: 0
• Posts: 11
• Joined: 31-May 12

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 12:52 PM

can u give some more clarification on this program sir???

### #6 sepp2k

• D.I.C Lover

Reputation: 2307
• Posts: 3,568
• Joined: 21-June 11

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 01:06 PM

prasad manne, on 10 June 2012 - 09:52 PM, said:

can u give some more clarification on this program sir???

It's illegal and it might do anything. There's really nothing that can be clarified further about that.

### #7 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,997
• Joined: 30-May 10

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 01:38 PM

POPULAR

> the following is the program which i have come across and tried to solve:
The only real difficulty with these kinds of questions is being stuck with a tutor who claims to "know" the answer.

As has already mentioned, such expressions lead to undefined behaviour (UB). At this point, all bets are off and any answer your code produces is just as invalid as any other answer. Once you understand why it invokes UB, you can stop worrying about wasting any more time on a fruitless analysis.

With regard to tutors, you have a few choices.
- figure out which compiler they're using, then use that to give the answer they expect.

Feel free to point them to this link
http://www.dreaminco...ost__p__1042539

A better answer longer term is to find another tutor. If they can't get this right (no doubt they use void main, gets(), fflush(stdin) and a whole range of other horrors), you've got to wonder whether anything they say has any meaning at all outside of their microscopic world.

• Saucy!

Reputation: 6237
• Posts: 24,002
• Joined: 23-August 08

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 01:48 PM

Entertaining bets on Turbo C....

### #9 ishkabible

• spelling expret

Reputation: 1702
• Posts: 5,861
• Joined: 03-August 09

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 03:11 PM

I don't think anyone is going to bet against you short of <conio.h> that's a dead ringer for Turbo C sir

This post has been edited by ishkabible: 10 June 2012 - 03:12 PM

### #10 jon.kiparsky

• Pancakes!

Reputation: 9512
• Posts: 16,467
• Joined: 19-March 11

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 04:49 PM

Quote

is this the correct solution sir????

Just out of curiousity... why didn't you just compile it and see?

### #11 Skydiver

• Code herder

Reputation: 4822
• Posts: 15,946
• Joined: 05-May 12

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 05:43 PM

jon.kiparsky, on 10 June 2012 - 04:49 PM, said:

Quote

is this the correct solution sir????

Just out of curiousity... why didn't you just compile it and see?

How would compiling and running it help? Since this is undefined behavior, the compiler is free to do anything it wants to as stated above. If the compiler decided to generate code that would play the Star Spangled Banner and flashing the screen red white and blue, would the correct response then be 1776?

This post has been edited by Skydiver: 10 June 2012 - 05:44 PM

### #12 jon.kiparsky

• Pancakes!

Reputation: 9512
• Posts: 16,467
• Joined: 19-March 11

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 05:48 PM

You're right, of course. But really, I don't understand how someone could ask a question without including something like "I tried it in my compiler and I got this answer..." as part of the post.
There's a certain baseline level of curiosity required to be a programmer, and this question suggests that we're not getting over that bar.

### #13 Skydiver

• Code herder

Reputation: 4822
• Posts: 15,946
• Joined: 05-May 12

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 10 June 2012 - 06:12 PM

True. I agree with you there, since I was equally trying to elicit the same kind of curiosity in another thread where somebody seemed to just be copying and pasting code without understanding what it does.

### #14 cfoley

• Cabbage

Reputation: 2338
• Posts: 4,889
• Joined: 11-December 07

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 11 June 2012 - 04:11 AM

I can understand this being illegal but why does the standard settle for undefined behaviour? Wouldn't it make things easier if it were defined as a compilation error? Am I missing something important? Is it a shortcoming of the standard or am I just getting too used to Java's security blanket?

### #15 jon.kiparsky

• Pancakes!

Reputation: 9512
• Posts: 16,467
• Joined: 19-March 11

## Re: evaluating i++ + ++i + i++ + ++i * --i + i++ / ++i;

Posted 11 June 2012 - 05:02 AM

Since there were already a number of implementations in place before the standard was written, there were probably several conflicting interpretations in play, and no way to reasonably standardize on one of them. Jave's standard has the advantage of having been written either before or alongside the reference implementation - and also by people who had learned from previous experience with standards, notably Steele.

• (2 Pages)
• 1
• 2

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}