tags:

views:

100

answers:

5

Hi All,

This is a small piece of code shown below which is using do while loops. I really dont understand the unexpected behaviour i see when i execute this code.This code shown below uses a do while loop condition and from my knowledge a do while loops executes in such a way that if first executes the statement and then checks in the while part if the condition is true or not.But when i execute this code it never comes out of the loop and goes on forever.Can anyone please tell me why is it happening?

           unsigned int A = 5;
           int B = 0;
           main()
          {
               do
               {
                   A = A + 5;
                   B = B - 1;
               }while(B > A);

           printf("hello\n");
          }

Now i never get the "hello" printed.

Thanks in advance Maddy

+1  A: 

You're comparing an unsigned int to a negative number. Try turning A into a signed int.

On Freund
+1  A: 

Comparing signed and unsigned can lead to confusing behavior. You should make both A and B the same type.

R Samuel Klatchko
A: 

prints "hello" for me. takes a while (well, a second or two).

modified to this...

void main()
{
    unsigned int A = 5;
    unsigned int loop = 0;
    int B = 0;
    do
    {
        A = A + 5;
        B = B - 1;
        loop++; 
    }while(B > A);

    printf("hello %d  %d %d\n", B, A, loop);
}

prints out :-

hello -715827882  -715827881 715827882
Keith Nicholas
Thanks a lot everyone.It took a long time than expected but eventually it had printed "hello".
maddy
+3  A: 

As the other answers say, comparing a signed and unsigned value is a bad idea (GCC will warn about it if given the right flags). The reason you're stuck in an "infinite loop" is the signed value B is treated as unsigned for the purpose of the comparison. The first time you get to while(B > A), B is -1 (it started at 0 and was decremented once). When treated as unsigned, it becomes the largest possible integer, so the comparison is true. It remains true for a long time, so the loop appears to never end. Eventually incrementing A and decrementing B would cause them to pass each other and the loop would end, but it will take longer than you anticipated

Michael Mrozek
@Michael...Thanks for the solution,but what i am a bit confused is how come an signed number -1 when converted to an unsigned one becomes the largest integer?I mean in what way the conversion is done?
maddy
@maddy - in two complement arithmetic, the signed value -1 has a bit pattern of all 1's. If you interpret that bit pattern as unsigned, it will be the largest value that is representable.
R Samuel Klatchko
@maddy Generally negative numbers are stored in a format called [two's complement](http://en.wikipedia.org/wiki/Two's_complement); it happens that in that form -1 is stored as 0xFFFFFFFF (on 32-bit machines). As an unsigned number, that's the largest possible number
Michael Mrozek
Thanks a lot Samuel and Michael.
maddy
A: 

You are comparing signed with unsigned. But you will see "hello" eventually. As A and B will both overflow to make B > A false.

Jason
They don't need to, they can pass each other normally (B is being decremented from MAX_UINT while A is incremented from 0)
Michael Mrozek