I read K & R; so far i'm fine, but there is something in the function itoa()that i don't understand. Here, in itoa(), they say that they themselves change the numbers. For example, 10 is 01 (they change the line):
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0)
n = -n;
i = 0;
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
return;
}
I do not understand how this changed the number. Despite what we just do n % 10 + '0', then its next digit, which 10 then 1 is removed, then it goes to 0 right? Or do I not understand his logic?
source
share