Skip to content

Commit 98dd84c

Browse files
committed
tweak answer
Some minor tweaks and added a reference to an alternative approach.
1 parent d0e4da4 commit 98dd84c

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

chapter03/3-4.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@
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+
* By Faisal Saadatmand
8+
*/
9+
10+
/*
711
* 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.
1822
*
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.
2025
*/
2126

2227
#include <stdio.h>
@@ -39,18 +44,19 @@ void itoa(unsigned n, char s[])
3944
i = 0;
4045
do { /* generate digits in revered order */
4146
s[i++] = n % 10 + '0'; /* get next digit */
42-
} while ((n /= 10) > 0); /* delete it */
47+
} while ((n /= 10) != 0); /* delete it */
4348
if (sign < 0)
4449
s[i++] = '-';
4550
s[i] = '\0';
4651
reverse(s);
4752
}
4853

54+
/* reverse: reverse the order of the characters in the string s */
4955
void reverse(char s[])
5056
{
5157
int c, i, j;
5258

53-
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
59+
for (i = 0, j = strlen(s) - 1; i < j; ++i, --j) {
5460
c = s[i];
5561
s[i] = s[j];
5662
s[j] = c;
@@ -61,6 +67,8 @@ int main(void)
6167
{
6268
char str[MAXLEN];
6369

70+
itoa(0, str); /* do-while loop is necessary for 0 */
71+
printf("%i -> %s\n", 0, str);
6472
itoa(INT_MAX, str);
6573
printf("%i -> %s\n", INT_MAX, str);
6674
itoa(INT_MIN, str);

0 commit comments

Comments
 (0)