I do not understand itoa () in the book K & R

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) /* record sign */
        n = -n; /* make n positive */
    i = 0;
    do { /* generate digits in reverse order */
        s[i++] = n % 10 + '0'; /* get next digit */
    } while ((n /= 10) > 0); /* delete it */
    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?

+5
source share
3 answers

In a do-while loop, it pulls the numbers on top (the least significant digit). So, if you have the number -123456789, it processes 9, then 8, then 7, etc.

, - ( ), "987654321-", .

+12

n % 10 0 n = 10, s 01.

reverse() .

+2

. , , , - , "" . , , (), , .

- :

decimal_digits = (int)log10( n ) + 1 ;

FPU ( FPU), , , , .

0

All Articles