Subj : Re: Reversing a number To : comp.programming From : Peter Nilsson Date : Tue Sep 13 2005 01:12 am zeroxia@gmail.com wrote: > Is this implementation not compiler-specific? > (For example, the case of which i is negative. > > int reverse(int i) > { int n = 0; > while (i) > { > n *= 10; > n += i % 10; > i /= 10; This can cause problems in the 1990 flavour of C as the rounding of division is implementation defined if either operatand is negative. In other words, -4 % 10 may produce +6 rather than -4. On such a machine, you get an indefinite loop, and consequent overflow, since -1/10 is just -1 again. > } > return n; > } -- Peter .