Thread: Problems in c

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Post Problems in c


    Hi! I am new to c and I am facing lols of problems. Here I am introducing one of those problems and I really need help to understand how the compiler compile the following:

    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int i = 0, j;
    	j =i + ++i + ++i;
    	printf ("%d\n", j);
    	return 0;
    }
    output is : 4

    Why the output is 4 and how? What is going on in j = i + ++i + ++i;
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    Why do you care? You simply should not write code like that; it is easy to avoid. In fact you have to try extra hard just to write such pointless code.

    This is exactly the kind of pointless question beloved of setters of C homework assignments, and therefore your question looks like a thinly disguised attempt to get your homework done.

    I sympathise with you if you have a tutor who thinks that this is a useful assignment, but all you need learn from this is that the rules regarding expression evaluation and sequence points with post- and pre- increment/decrement are sufficiently arcane for you to know to avoid using them in that way.

    Ask yourself; when would I ever want to write code like that and for what purpose?

    Follow this simple rule:
    "never use post or pre increment/decrement in an expression involving any other operators or more than one variable."
    This restricts usage to expressions of the form:

    Code:
    i++ ;
    ++i ;
    i-- ;
    ++i ;
    and in C at least means that it makes no difference whether your use pre-or post form, so choose one and stick to that exclusively.

    The thing is ++ and -- are assignment operators. They modify the argument, and to use an argument that will change during expression evaluation is a path to all sorts of confusion - the ambiguity being when will the variable change value? The solution is not to answer the question, but rather to avoid the problem.

    Your original expression may have intended:
    Code:
    ++i ;
    ++i ;
    j = i + i + i ;
    or
    Code:
    j = i + (i + 1) + (i + 2) ;
    or
    Code:
    j = i + (i + 1) + (i + 1) ;
    or
    Code:
    j = i + (i + 2) + (i + 2) ;
    Or something else altogether. Who knows? It is not clear what the writer intended so it is not good code.

    If the actual result obtained in this case was in fact what was intended then it would have been better written as follows:

    Code:
    j = i + (i + 2) + (i + 2) ;  // or i * 3 + 4 ;
    i = i + 2 ;
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    With all due respect (refer to Woody Allen's use of the term in his movies, starting with Broadway Danny Rose), why are you attempting such an idiotically stupid thing?

    j =i + ++i + ++i;

    Refer to http://en.wikipedia.org/wiki/Sequence_point, as well as my own http://pgm.dwise1.net/misc/test.html. Pay especial attention to my own link.

    Are you in any way familiar with the expression, "All bets are off!"? Well, when you go changing a variable's value in the middle of an expression, then "all bets are off!" Nothing is guaranteed. In other words, this is something that you should never ever even begin to dream of doing.

    Please tell us just exactly why you are trying to do this thing. Seriously, who told you to attempt this abomination! Was it your instructor? Tell us! Was it your textbook? Tell us! Just who exactly is it who tempt you beginners to attempt these unspeakable abominations? We programming masters need to confront them for their unspeakable practices!

    Please pay particular attention to my own treatment of this type of problem in order to understand what is happening.

    And just in case you are idiotic enough to not follow my link, there are two different ways in which the variables could be treated, so when you try to pull stupid crap like this, you cannot know what the outcome will be. The purpose of standard software is that you get the same results no matter what. No matter what! Are you starting to get the idea?

IMN logo majestic logo threadwatch logo seochat tools logo