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

    Join Date
    Nov 2012
    Posts
    21
    Rep Power
    0

    Types, Operators and Expression Code Example!


    Hey guys,

    I have this code and the output for this code is:

    integer0 = 3, integer1 =5,integer2 = 9, integer3 =6.

    I wanna understand why the integer1 is equal 5 and integer2 equal 9. The other makes sense.

    Thank you very much.

    g Code:
    #include <stdio.h>
    #define ITERATIONS 3
     
    void main(void)
    {
    int integer0 = 0, integer1 = 1, integer2 = 2, integer3 =3;
     
    for(;integer2++, integer0 < ITERATIONS ;)
    integer1 = (integer0++, ++integers2, integer3++);
    printf("integer0 = %1d, integer1 =%1d," 
             "integer2 = %1d, integer3 =%1d.\n", integer0, integer1, integer2, integer3);
     
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    If I recall correctly, the comma operator has lowest precedence, evaluates from left to right, and has the value of the right hand expression.

    Also, the value of the post-increment operator is the value of the variable. The variable changes afterward.

    Therefor the final value of integer1 will be 1 less than the final value of integer3, which you say you understand.

    integer2 starts at 2. You then increment it twice per cycle over three cycles bring it to 8. Bring your smart computer to the condition statement:

    integer2++, integer0 < ITERATIONS;

    integer2 increments once again, to 9, and integer0 is no longer less than ITERATIONS, so the condition is 0 and the loop terminates.

    Don't write code like this.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    21
    Rep Power
    0
    Thank you b49P23TIvg!

    But why integer2 inkrements once again, to 9 and integer1 to 5.

    Why integer2 increments once again, to 9? And integer3 to 6?? :confused:
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    My explanation has caused you to doubt your understanding of integer3, which you previously claimed to understand. Furthermore, you still don't understand integer1 and integer2. I quit.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    21
    Rep Power
    0
    You are right! I will try it tomorrow again...Thanx...!

IMN logo majestic logo threadwatch logo seochat tools logo