Skip to content

Commit 3ec1939

Browse files
committed
Fixed itoa's interface
1 parent 774e77b commit 3ec1939

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

Diff for: chapter04/4-12.c

+16-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/*
1010
* NOTE: We could have made i a static variable; however, that is problematic
1111
* because its value will persists throughout the entire life of the program, and
12-
* thus, multiple calls to itoa will use the value of i from the previous call,
12+
* thus, multiple calls to itoar will use the value of i from the previous call,
1313
* which will result in an undesirable outcome.
1414
*/
1515

@@ -20,18 +20,24 @@
2020
#define MAXLEN 1000
2121

2222
/* functions */
23-
int itoa(unsigned, char [], int);
23+
void itoa(int, char []);
24+
int itoar(int, char [], int);
2425

25-
int itoa(unsigned n, char s[], int i)
26+
/* itoa: interface function to itoar */
27+
void itoa(int n, char s[])
2628
{
27-
int sign;
29+
itoar(n, s, 0);
30+
}
2831

29-
if ((sign = n) < 0) {
32+
/* itoar: convert n to characters in s, recursive version */
33+
int itoar(int n, char s[], int i)
34+
{
35+
if (n < 0) {
3036
s[i++] = '-';
3137
n = -n;
3238
}
3339
if (n / 10)
34-
i = itoa(n / 10, s, i); /* recursive call */
40+
i = itoar(n / 10, s, i); /* recursive call */
3541
s[i++] = n % 10 + '0';
3642
s[i] = '\0';
3743
return i; /* return the updated value of i */
@@ -41,16 +47,16 @@ int main(void)
4147
{
4248
char str[MAXLEN];
4349

44-
itoa(996, str, 0);
50+
itoa(996, str);
4551
printf("%s\n", str);
4652

47-
itoa(-2345, str, 0);
53+
itoa(-2345, str);
4854
printf("%s\n", str);
4955

50-
itoa(INT_MAX, str, 0);
56+
itoa(INT_MAX, str);
5157
printf("%s\n", str);
5258

53-
itoa(INT_MIN, str, 0);
59+
itoa(INT_MIN, str);
5460
printf("%s\n", str);
5561

5662
return 0;

0 commit comments

Comments
 (0)