diff options
author | sinanmohd <pcmsinan@gmail.com> | 2022-06-04 12:18:08 +0530 |
---|---|---|
committer | sinanmohd <pcmsinan@gmail.com> | 2022-06-04 12:18:08 +0530 |
commit | 00af3bb6a98925c1b997f3b077615627e31ba24c (patch) | |
tree | 3581c55e65488596a2b3863face1aabeef8a28c1 | |
parent | c24973af02bc33f2f5f25d37e22ca91da5de3c47 (diff) |
fix for most -ve int
-rw-r--r-- | 3.4.c | 7 |
1 files changed, 4 insertions, 3 deletions
@@ -8,7 +8,7 @@ char* itoa(int n, char str[]); int main(void) { - int n = -2147483648; + int n = 2147483647; char str[MAXLEN]; printf("%d\n", n); @@ -38,8 +38,8 @@ itoa(int n, char str[]) { int sign, i; - if ((sign = n) < 0) - n = -n; + /* to include the most -ve int*/ + n = ((sign = n) < 0) ? -(n+1) : n-1; i = 0; do { @@ -49,6 +49,7 @@ itoa(int n, char str[]) if (sign <0) str[i++] = '-'; + str[0] += 1; str[i] = '\0'; reverse(str); |