From 1d958679201f143ecf1c5527bd462e29b87427de Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sat, 4 Dec 2021 08:46:22 +0800 Subject: 3.4 --- 3/4.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to '3/4.c') diff --git a/3/4.c b/3/4.c index 5ddf2ef..91c3953 100644 --- a/3/4.c +++ b/3/4.c @@ -4,14 +4,10 @@ #define MAXLEN 50 -// xplanation of the error: -// In itoa, for largest negative number, n = -n overflows -// resulting in n = INT_MIN (wraps around) causing a god awful mess. - void itoa(int n, char s[]); void reverse(char s[]); -/* Fixes itoa function to handle the largest negative number */ +/* fixes itoa function to handle the largest negative number */ int main(int argc, char *argv[]) { char s[MAXLEN]; @@ -27,12 +23,14 @@ void itoa(int n, char s[]) { i = 0; - if (n != INT_MIN) { + if (n >= 0) { do { s[i++] = n % 10 + '0'; } while ((n /= 10) > 0); } else { + // avoid integer overflow of n = -n of min n in original impl + // by handling negtive values separately. do { s[i++] = -(n % 10) + '0'; } while ((n /= 10) <= -1); -- cgit v1.2.3