summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--3/4.c7
-rw-r--r--3/6.c7
2 files changed, 8 insertions, 6 deletions
diff --git a/3/4.c b/3/4.c
index 6bcc1bb..90a970a 100644
--- a/3/4.c
+++ b/3/4.c
@@ -19,11 +19,12 @@ int main(int argc, char *argv[]) {
}
void itoa(int n, char s[]) {
- int i;
+ int i, sign;
i = 0;
+ sign = n < 0;
- if (n >= 0) {
+ if (!sign) {
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
@@ -36,7 +37,7 @@ void itoa(int n, char s[]) {
} while ((n /= 10) <= -1);
}
- if (n < 0)
+ if (sign)
s[i++] = '-';
s[i] = 0;
diff --git a/3/6.c b/3/6.c
index bb3636f..846b7fa 100644
--- a/3/6.c
+++ b/3/6.c
@@ -40,11 +40,12 @@ void reverse(char s[]) {
}
void itoa(int n, char s[], int w) {
- int i;
+ int i, sign;
i = 0;
+ sign = n < 0;
- if (n >= 0) {
+ if (!sign) {
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
@@ -54,7 +55,7 @@ void itoa(int n, char s[], int w) {
} while ((n /= 10) <= -1);
}
- if (n < 0)
+ if (sign)
s[i++] = '-';
while (i < MAXLEN - 1 && i < w)