4
4
* equal to -(2^wordsize-1). Explain why not. Modify it to print that value
5
5
* correctly, regardless of the machine on which it runs.
6
6
*
7
+ * By Faisal Saadatmand
8
+ */
9
+
10
+ /*
7
11
* ANSWER:
8
- * In two's complement number representation, the limit for a signed int ranges
9
- * from -(2^wordsize-1) to (2^wordsize-1)-1. The most significant bit is the
10
- * sign bit; however, it also holds a value for negative numbers; thus, making
11
- * the negative limit larger than the positive limit by a value of 1. When we
12
- * negate the largest negative number, -(2^wordsize-1), we get a number that is
13
- * equal to 2^wordsize-1, which is larger than the largest positive number,
14
- * (2^wordsize-1)-1. This will overflow the signed int and cause unexpected
15
- * results. To overcome this, we can use an unsigned int for n. The check for
16
- * whether n is negative or not is taking care of with the assigned of n to the
17
- * int variable sign, in which n is convert to a signed int.
12
+ * In two's complement number representation, the range of values an int can
13
+ * hold is from -(2^wordsize-1) to (2^wordsize-1)-1. The most significant bit
14
+ * is the sign bit; however, it also holds a value for negative numbers; thus,
15
+ * making the negative limit larger than the positive limit by a value of 1.
16
+ * When we negate the largest negative number, -(2^wordsize-1), we get a number
17
+ * that is equal to 2^wordsize-1, which is larger than the largest positive
18
+ * number, (2^wordsize-1)-1. This will overflow a signed int and cause
19
+ * unexpected results. To overcome this, we can use an unsigned int for n. The
20
+ * check for whether n is negative or not is taking care of with the assigned
21
+ * of n to the int variable sign, in which n is convert to a signed int.
18
22
*
19
- * By Faisal Saadatmand
23
+ * Alternatively, we could avoid negating n altogether and use the abs function
24
+ * from stdlib.h to extract the digits. See 3-4a.c.
20
25
*/
21
26
22
27
#include <stdio.h>
@@ -39,18 +44,19 @@ void itoa(unsigned n, char s[])
39
44
i = 0 ;
40
45
do { /* generate digits in revered order */
41
46
s [i ++ ] = n % 10 + '0' ; /* get next digit */
42
- } while ((n /= 10 ) > 0 ); /* delete it */
47
+ } while ((n /= 10 ) != 0 ); /* delete it */
43
48
if (sign < 0 )
44
49
s [i ++ ] = '-' ;
45
50
s [i ] = '\0' ;
46
51
reverse (s );
47
52
}
48
53
54
+ /* reverse: reverse the order of the characters in the string s */
49
55
void reverse (char s [])
50
56
{
51
57
int c , i , j ;
52
58
53
- for (i = 0 , j = strlen (s ) - 1 ; i < j ; i ++ , j -- ) {
59
+ for (i = 0 , j = strlen (s ) - 1 ; i < j ; ++ i , -- j ) {
54
60
c = s [i ];
55
61
s [i ] = s [j ];
56
62
s [j ] = c ;
@@ -61,6 +67,8 @@ int main(void)
61
67
{
62
68
char str [MAXLEN ];
63
69
70
+ itoa (0 , str ); /* do-while loop is necessary for 0 */
71
+ printf ("%i -> %s\n" , 0 , str );
64
72
itoa (INT_MAX , str );
65
73
printf ("%i -> %s\n" , INT_MAX , str );
66
74
itoa (INT_MIN , str );
0 commit comments