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

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1455 Views - Last Post: 11 June 2012 - 05:11 AM Rate Topic: -----

#1 prasad manne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • 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.
Was This Post Helpful? 5
  • +
  • -

#3 prasad manne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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???
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

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

Posted 10 June 2012 - 12:48 PM

View Postprasad 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

Was This Post Helpful? 3
  • +
  • -

#5 prasad manne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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???
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

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

Posted 10 June 2012 - 01:06 PM

View Postprasad 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.
Was This Post Helpful? 1
  • +
  • -

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • 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.
- consult a mind reader

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.
Was This Post Helpful? 10
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • 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....
Was This Post Helpful? 0
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • 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 :P 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

Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7752
  • View blog
  • Posts: 13,108
  • 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?
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,123
  • Joined: 05-May 12

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

Posted 10 June 2012 - 05:43 PM

View Postjon.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? :lol:

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

Was This Post Helpful? 1
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7752
  • View blog
  • Posts: 13,108
  • 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.
Was This Post Helpful? 2
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,123
  • 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.
Was This Post Helpful? 1
  • +
  • -

#14 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2001
  • View blog
  • Posts: 4,156
  • 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?
Was This Post Helpful? 1
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7752
  • View blog
  • Posts: 13,108
  • 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.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2