9
9
/*
10
10
* NOTE: We could have made i a static variable; however, that is problematic
11
11
* 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,
13
13
* which will result in an undesirable outcome.
14
14
*/
15
15
20
20
#define MAXLEN 1000
21
21
22
22
/* functions */
23
- int itoa (unsigned , char [], int );
23
+ void itoa (int , char []);
24
+ int itoar (int , char [], int );
24
25
25
- int itoa (unsigned n , char s [], int i )
26
+ /* itoa: interface function to itoar */
27
+ void itoa (int n , char s [])
26
28
{
27
- int sign ;
29
+ itoar (n , s , 0 );
30
+ }
28
31
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 ) {
30
36
s [i ++ ] = '-' ;
31
37
n = - n ;
32
38
}
33
39
if (n / 10 )
34
- i = itoa (n / 10 , s , i ); /* recursive call */
40
+ i = itoar (n / 10 , s , i ); /* recursive call */
35
41
s [i ++ ] = n % 10 + '0' ;
36
42
s [i ] = '\0' ;
37
43
return i ; /* return the updated value of i */
@@ -41,16 +47,16 @@ int main(void)
41
47
{
42
48
char str [MAXLEN ];
43
49
44
- itoa (996 , str , 0 );
50
+ itoa (996 , str );
45
51
printf ("%s\n" , str );
46
52
47
- itoa (-2345 , str , 0 );
53
+ itoa (-2345 , str );
48
54
printf ("%s\n" , str );
49
55
50
- itoa (INT_MAX , str , 0 );
56
+ itoa (INT_MAX , str );
51
57
printf ("%s\n" , str );
52
58
53
- itoa (INT_MIN , str , 0 );
59
+ itoa (INT_MIN , str );
54
60
printf ("%s\n" , str );
55
61
56
62
return 0 ;
0 commit comments