I was trying to do a factorial of a 100 on C and integer overflow happened, but what I didn't understand was why my unsigned variable became negative.
int main(void)
{
unsigned long value =1;
for(unsigned long n = 0;n<100;n++){
printf("This n %ld\n",value);
value *= (n+1);
}
printf("%ld\n",value);
return 0;
}
With unsigned variables, after the variable capacity is overflowed, shouldn't it start from 0 again?
The first values of value:
This current value 2
This current value 6
This current value 24
This current value 120
This current value 720
This current value 5040
This current value 40320
This current value 362880
This current value 3628800
This current value 39916800
This current value 479001600
This current value 6227020800
This current value 87178291200
This current value 1307674368000
This current value 20922789888000
This current value 355687428096000
This current value 6402373705728000
This current value 121645100408832000
This current value 2432902008176640000
This current value -4249290049419214848
How can this be possible?