The below function confuses me. Basically, since unsigned integers use a two's complement system, we cannot take the inverse of n before processing it, should n be negative. Because the largest negative number does not have absolute value representation. Thus, we use abs here after processing n % 10 while n is still negative. But how does that while loop work? Why doesn't it loop forever? If n is -133, reducing it by a factor of 10 with each iteration, it will start spitting out -1 infinitely. Am I missing something?
Code:
void itoa(int n, char s[]) {
int i, sign;
sign = n;
i = 0;
do {
s[i++] = abs(n % 10) + '0';
} while ( n /= 10 );
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}