0

The below code runs perfectly.Gives the correct output but, the moment I change the sign of the variables from signed to unsigned the program runs into an infinite loop. The program is to find the factorial of integers. The value of any variable doesn't get negative anywhere I am aware of the modular behavior of the unsigned int.

#include<stdio.h>

int main(void)
{
    int a[200], i,index, number, next, count, temp, test, x;

    scanf(" %d", &test);

    while(test--)
    {
        scanf(" %d", &number);
        a[0]=1;
        count=1;    //1 digit
        for(next=2;next<=number;++next)
        {
            index=0;temp=0;
            for(i=0;i<count;++i)
            {
                x=a[index]*next+temp;
                a[index]=x%10;
                temp=x/10;
                ++index;
            }
            while(temp!=0)
            {
                a[count++]=temp%10;
                temp=temp/10;
            }
        }
        for(i=count-1;i>=0;--i)
        printf("%d",a[i]);
        printf("\n");
    }
    return 0;
}
MrTux
  • 32,350
  • 30
  • 109
  • 146
Ashutosh Narang
  • 405
  • 4
  • 8

3 Answers3

7

The problem is that the loop

for(i=count-1;i>=0;--i)

will never exit if i is unsigned, because an unsigned integer is always greater than or equal to zero and thus the >= 0 test is always true.

3

An unsigned int can never be negative, so i >= 0 holds true all the time, that means that a loop like

unsigned int value;

value = 10;
while (value-- >= 0) {}

is effectively an inifinte loop, as well as your for (i = count - 1 ; i >= 0 ; --i) is.

Compilers do warn about this, so if you compile your code passing the right flags to the compiler, it should tell you that the condition will be always true.

You should note that while the >= makes no difference in this respect,

unsigned int value;

value = 10;
while (value-- != 0) {}

does work, as while (value-- > 0) also does, because value can be 0, but it can't be < 0.

There is also, no unsigned int integer overflow, so the loop will be infinite without causing undefined behavior, this answer has an explanation of what happens when you add 1 to the maximum value, if you subtract one from the minimum value, then I am sure you can "guess" what would happen from the same answer.

Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • I think you meant to say `i >= 0` holds true. – Jens May 23 '15 at 20:24
  • `(i = 0) > 0 == false` The problem is in the for loop testing for `i >= 0`. – datenwolf May 23 '15 at 20:25
  • @Jens right, it could be `0` at some point so `i > 0` is not always true. – Iharob Al Asimi May 23 '15 at 20:35
  • That explains it well. Thanks. – Ashutosh Narang May 24 '15 at 21:19
  • @AshutoshNarang you could accept the answer that you think worked better for you, it seems that the one you accepted is the one that worked, but also remember that this site is visited by programmers seeking for solutions and they often use answers from previous questions. – Iharob Al Asimi May 24 '15 at 21:50
  • To click the tick means to accept the answer? How can I use this site efficiently?To improve competitive programming skills. – Ashutosh Narang May 26 '15 at 16:55
  • The variables work like an odometer.Once the reach their reset point the values start repeating.In the case of unsigned variables the reset point is zero(0). So whenever (after reaching zero(0)), I trt to decrement it further it reaches the other side of its limit in this case 65535 for an int. – Ashutosh Narang May 26 '15 at 17:03
  • @AshutoshNarang I wouldn't encourage competitive programming, since it's of no use, I would like you to learn useful programming and to write useful programs instead. – Iharob Al Asimi May 26 '15 at 18:22
  • Open Source?? I know C and C++. I think these are really good languages and I want to build great stuff using these during my college days and ahead. I have learn't these languages on my own(Books and Online Resources), but they are very limited in implementation? How should I go about learning practical stuff? – Ashutosh Narang May 26 '15 at 19:57
  • They are illimited, c++ is ugly and hard to learn but it's useful for some stuff. A then don't understande the comment about competitive programming? did you mean *competent*? – Iharob Al Asimi May 26 '15 at 19:59
  • I meant competitive programming , as in Online Judges. – Ashutosh Narang May 26 '15 at 20:01
  • That's a waste of time. – Iharob Al Asimi May 26 '15 at 20:01
  • How should I go about developing some GOOD programs. Can you list out the resources? – Ashutosh Narang May 26 '15 at 20:10
  • You just get in a project that requires you to do something that you don't know, if you are smart enough you will end up learning and doing it. – Iharob Al Asimi May 26 '15 at 20:11
  • I am going to start working some projects on Github. But How do I get involved in a project there? – Ashutosh Narang May 26 '15 at 20:13
1

As other answers already noted, the loop

for(i=count-1;i>=0;--i)

is an infinite loop if i is unsigned. It can be rewritten in a different form as

for (i = count - 1; i != -1; --i)

which will work as intended for both signed and unsigned i. However, some might find it less readable than the original.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765