something weird

This is a discussion on something weird within the C Programming forums, part of the General Programming Boards category; hello today i was trying something with linked list to initialize a var in the 2nd field of the for ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    something weird

    hello today i was trying something with linked list to initialize a var in the 2nd field of the for loop using the , operator but i got into a forever loop i want to know if this is defined behaviour or not ? since 1st field only does stuff once and the 3rd field does it after loop runs
    Code:
    #include <stdio.h>
    int main(void)
    {
        int x=1;
        int b;
        for(; x<10  , b=x  ;x++ )
            printf("%d\n",x);
        getchar();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    you are setting b equal to x, I believe that is an assignment not a comparison. It should always be true correct?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not an infinite loop, it will eventually break when b == 0. You loop basically says:

    for <initialize nothing> ; so long as (discarded: x<10) b=x is non-zero; increment x


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    The results are well defined: look up the comma operator.

    The expression "p, q" evaluates p, then evaluates q, and yields a result of q. Similarly x < 10, b = x evaluates x < 10 first, then assigns b to be x, and yields the value of b. b (like x) is always non-zero - or will be non-zero for quite a few iterations - so the loop never terminates.

    Try changing the loop to
    Code:
       for(; c = (x<10  , b=x)  ;x++ )
    This is exactly the same, except it catches the value of the expression "x < 10, b = x" in the variable c. If you watch the value of c, you will find that it holds the value of x each time through the loop.
    Last edited by grumpy; 09-24-2009 at 07:38 PM.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What he really should be doing is this:
    Code:
    for( ; b = x && x < 10; x++ )
    Otherwise, it's still not going to break when x is bigger than 9.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by quzah View Post
    What he really should be doing is this:
    Code:
    for( ; b = x && x < 10; x++ )
    Otherwise, it's still not going to break when x is bigger than 9.


    Quzah.
    i think that wont work since it will evalute to false and wont run the loop i tried it and it didn't run the loop

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    b = x will only evaluate as false if x is 0.

    EDIT: Well, that's true I suppose, but not the point. = has lower precedence than && so, x && x < 10 will be evaluated and assigned to b instead. Sorry.
    Last edited by tabstop; 09-24-2009 at 09:31 PM.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oke here i got it i think when i puted braces around b=x i think it doesnt makes it get discarded and gets initialized to x then after that test is made
    Code:
    #include <stdio.h>
    int main(void)
    {
        int x;
        int b;
        for(x=0; (b=x) , x<10 ;x++)
              printf("%d\n",b);
        getchar();
        return 0;
    }

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    ...And now you're back to where you started. Here's an idea: how about not using obscure loop control constructs?

    Code:
        for(x = 0; x < 10; x++)
        {
            b = x;
            printf("%d\n",b);
        }
    Easy.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by Sebastiani View Post
    ...And now you're back to where you started. Here's an idea: how about not using obscure loop control constructs?

    Code:
        for(x = 0; x < 10; x++)
        {
            b = x;
            printf("%d\n",b);
        }
    Easy.
    yah i know thats easy :P but i was just curious abt initializing in the middle when i added (b=x) in braces it made it work ..

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It wasn't the braces that made it work, it was putting it before the comma.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yes your right it makes sense aswell

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Although the three expressions of a for-clause can be anything, it is normally considered proper for the first and third expressions to be manipulators of state, and the second expression to be an idempotent boolean expression. In other words, changing state inside the conditional expression is really, really bad form.

    This code isn't really clever, it's just confusing (obviously confusing enough for you to code up this bug). I've seen some rather complicated expressions in for-clauses but nothing like that before. Don't do it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 10:23 PM
  2. weird
    By kiz in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:16 AM
  3. Weird Characters With GetDlgItemText
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2006, 04:53 PM
  4. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 07:32 AM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21