I read a book on C, there is such an example and a test task for it:
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); }
3-4. bla-bla-bla ... itoa will not be able to handle the largest negative number equal to
-(2^wordsize-1)
. Explain why this is happening.
I'm still very tight with the presentation of the processes actually taking place. Such a number cannot be processed, because (roughly speaking) there is no corresponding positive for it and INT_MAX (or analog, if done for some long) is one less than what should be the result of n = -n
? If so, what at this moment is written into the variable n? 0x0 ... 01? 0xF0 ... 0? Just INT_MAX? Will it depend on the particular machine?