Thread: Explanation on a simple program

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    Explanation on a simple program

    hey everyone,
    Can anyone explain to me why x=4 in the following code?

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int x,y,z;
        
        x=2;
        y=2;
        
        x=2*(y++);
        z=2*(++y);
        
        printf("x=%d\ny=%d\nz=%d\n", x,y,z);
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yes - but this should be a simple enough of an exercise to be able to solve yourself.

    Try figuring it out and tell us what you think the value of 'x' should be.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by Matticus View Post
    Yes - but this should be a simple enough of an exercise to be able to solve yourself.

    Try figuring it out and tell us what you think the value of 'x' should be.
    from what i see:
    x=2;
    x= 2*3=6; because y++=3;

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Note that there is a distinct difference between "++y" and "y++". The first is a "pre-increment" and the second is a "post-increment".

    "Pre-increment" increments the value before it is used in an expression.
    "Post-increment" increments the value after it is used in an expression.

    Does this help clear up the confusion, or do you need further explanation?

    (3.12a)

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by Matticus View Post
    Note that there is a distinct difference between "++y" and "y++". The first is a "pre-increment" and the second is a "post-increment".

    "Pre-increment" increments the value before it is used in an expression.
    "Post-increment" increments the value after it is used in an expression.

    Does this help clear up the confusion, or do you need further explanation?

    (3.12a)
    okay so why is the result 4 when it should be 8 then.
    since ++y is already 3 before it's used then y++ so y=4
    2*4=8
    correct me if i'm wrong please

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    since ++y is already 3 before it's used then y++ so y=4
    But in the program you posted, "y++" comes first.

    In that case, the current value of 'y' is used before it is incremented.

    So on line 11, x = 2 * 2 = 4. Now that the expression is complete, 'y' is incremented to three.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    ohh okay so if i understand

    x=2*(y++)= 2*2
    y=3;

    is that the case?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yes, it seems you have it!
    Now analyze line 12 and see if you understand how that result is determined.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    okay well since now y=3 and we have ++y which is initialized before it's use y=4 therefor answer is 8

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Correct! Well done!

    (Note: The terminology in your post should be "incremented" and not "initialized")

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    Quote Originally Posted by Matticus View Post
    Correct! Well done!

    (Note: The terminology in your post should be "incremented" and not "initialized")
    thanks for the help

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-29-2012, 08:45 PM
  2. Replies: 1
    Last Post: 04-09-2012, 07:54 PM
  3. Replies: 6
    Last Post: 05-16-2011, 05:12 AM
  4. Program explanation.
    By nullifyed in forum C Programming
    Replies: 3
    Last Post: 06-18-2010, 02:29 AM
  5. Simple Explanation Needed
    By slowcoder in forum C Programming
    Replies: 7
    Last Post: 07-10-2007, 02:00 PM