Skip to content

Commit 4add853

Browse files
committed
fix answer and simplify code
1 parent 389b6e8 commit 4add853

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

chapter03/3-4.c

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
* equal to -(2^wordsize-1). Explain why not. Modify it to print that value
55
* correctly, regardless of the machine on which it runs.
66
*
7-
* Answer: In two's complement number representation, the limit ranges from
8-
* -(2^wordsize-1) to (2^wordsize-1)-1. The most significant bit is the sign
9-
* bit; however, it also holds a value for negative numbers; thus, making the
10-
* negative limit larger than the positive limit by a value of 1. This makes a
11-
* binary value of -(2^wordsize-1) equal to 2^wordsize-1, which is larger than
12-
* (2^wordsize-1)-1. For example, in a 8-bit wordsize, -128 and 128 are
13-
* represented as 0x80, which is larger than what the largest positive number
14-
* an 8-bit word can hold, 127 or 0x8f. The itoa function's first assignment
15-
* instruction is to negate n. In the case n is
16-
* -(2^worldsize-1), the negation will produce the same binary number, and
17-
* therefore, the algorithm will fails at the do/while loop.
7+
* 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.
1818
*
1919
* By Faisal Saadatmand
2020
*/
@@ -23,29 +23,25 @@
2323
#include <string.h>
2424
#include <limits.h>
2525

26-
#define MAXLEN 100
26+
#define MAXLEN 1000
2727

28-
void itoa(int, char []);
28+
void itoa(unsigned, char []);
2929
void reverse(char []);
3030

3131
/* itoa: convert n to characters in s */
32-
void itoa(int n, char s[])
32+
void itoa(unsigned n, char s[])
3333
{
3434
int i, sign;
3535

36-
i = 0;
37-
if ((sign = n) < 0) {
38-
s[i++] = -(n % 10) + '0'; /* extract last digit */
39-
n /= 10; /* reduce value to fit signed int */
40-
n = -n; /* negate value */
41-
}
36+
if ((sign = n) < 0) /* record sign */
37+
n = -n; /* make n positive */
4238

43-
do {
44-
s[i++] = n % 10 + '0';
45-
} while ((n /= 10) > 0);
39+
i = 0;
40+
do { /* generate digits in revered order */
41+
s[i++] = n % 10 + '0'; /* get next digit */
42+
} while ((n /= 10) > 0); /* delete it */
4643
if (sign < 0)
4744
s[i++] = '-';
48-
4945
s[i] = '\0';
5046
reverse(s);
5147
}
@@ -63,14 +59,12 @@ void reverse(char s[])
6359

6460
int main(void)
6561
{
66-
char string[MAXLEN];
67-
int intValue;
62+
char str[MAXLEN];
6863

69-
printf("Enter an integer to convert in a string (range: %i to %i):\n",
70-
INT_MIN, INT_MAX);
71-
scanf("%i", &intValue);
72-
itoa(intValue, string);
73-
printf("%s\n", string);
64+
itoa(INT_MAX, str);
65+
printf("%i -> %s\n", INT_MAX, str);
66+
itoa(INT_MIN, str);
67+
printf("%i -> %s\n", INT_MIN, str);
7468

7569
return 0;
7670
}

0 commit comments

Comments
 (0)