Skip to content

Commit 27e0c22

Browse files
committed
Add explanation and clean up
1 parent 4c5fc75 commit 27e0c22

File tree

1 file changed

+66
-38
lines changed

1 file changed

+66
-38
lines changed

chapter02/2-1.c

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
* and long variables, both signed and unsigned, by printing appropriate values
44
* from standard headers and by direct computation. Harder if you compute them:
55
* determine the ranges of the various floating-point types.
6+
*
7+
* For signed variables:
8+
* largest negative number is -(2^nbits-1)
9+
* largest positive number is (2^nbits-1)-1
10+
*
11+
* The loop iterates until the variable overflows the positive limit by 1. In
12+
* binary (two's complement), this results in achieving the largest negative
13+
* number with the loop's index and the max number with the result of the
14+
* calculation inside the loop minus 1. Note: we can also achieve the largest
15+
* positive number by negating the loop index and subtract one from it.
16+
* However; the cost of one extra variable (long long max), we gain in code
17+
* clarity.
18+
*
19+
* For unsigned variables:
20+
* min is 0
21+
* max (2^nbits)-1
22+
*
23+
* We do the same for the loop but we iterate the other way: we start from -1 all
24+
* the way up to 0.
25+
*
26+
* for floating point types:
27+
* refer to IEEE 754 standards
28+
*
629
* By Faisal Saadatmand
730
*/
831

@@ -17,70 +40,75 @@ void stdLibraryMacros(void);
1740

1841
void computeRanges(void)
1942
{
43+
long long unsigned int max;
44+
2045
/* characters */
21-
signed char c;
22-
int maxC = 1;
23-
for (c = 1; c > 0; c *= 2)
24-
maxC *= 2;
25-
printf("signed char\t\t%i\t\t\t%i\n", c, maxC - 1);
46+
signed char c;
47+
48+
for (c = max = 1; c > 0; c *= 2)
49+
max *= 2;
50+
printf("signed char\t\t%i\t\t\t%llu\n", c, max - 1);
2651

2752
unsigned char uc;
53+
2854
for (uc = -1; uc < 0; uc *= 2)
29-
;
30-
printf("unsigned char\t\t%2i\t\t\t%i\n", 0, uc);
55+
max *= 2;
56+
printf("unsigned char\t\t%2i\t\t\t%u\n", 0, uc);
3157

3258
/* Integers */
3359
signed short shrt;
34-
int maxShrt = 1;
35-
for (shrt = 1; shrt > 0; shrt *= 2)
36-
maxShrt *= 2;
37-
printf("signed short\t\t%i\t\t\t%i\n", shrt, maxShrt - 1);
60+
61+
for (shrt = max = 1; shrt > 0; shrt *= 2)
62+
max *= 2;
63+
printf("signed short\t\t%i\t\t\t%llu\n", shrt, max - 1);
3864

3965
unsigned short uShrt;
66+
4067
for (uShrt = -1; uShrt < 0; uShrt *= 2)
41-
;
42-
printf("unsigned short\t\t%2i\t\t\t%i\n", 0, uShrt);
68+
max += uShrt;
69+
printf("unsigned short\t\t%2i\t\t\t%u\n", 0, uShrt);
70+
71+
signed int i;
72+
73+
for (i = max = 1; i > 0; i *= 2)
74+
max *= 2;
75+
printf("signed int\t\t%i\t\t%llu\n", i, max - 1);
4376

44-
signed int sInt;
45-
int max_sInt = 1;
46-
for (sInt = 1; sInt > 0; sInt *= 2)
47-
max_sInt *= 2;
48-
printf("signed int\t\t%i\t\t%i\n", sInt, max_sInt - 1);
77+
unsigned int ui;
4978

50-
unsigned int uInt;
51-
for (uInt = -1; uInt < 0; uInt *= 2)
79+
for (ui = -1; ui < 0; ui *= 2)
5280
;
53-
printf("unsigned int\t\t%2i\t\t\t%u\n", 0, uInt);
81+
printf("unsigned int\t\t%2u\t\t\t%u\n", 0, ui);
5482

55-
signed long int lInt;
56-
signed long int max_lInt = 1;
57-
for (lInt = 1; lInt > 0; lInt *= 2)
58-
max_lInt *= 2;
59-
printf("signed long\t\t%li\t%li\n", lInt, max_lInt - 1);
83+
signed long int li;
6084

61-
unsigned long int ulInt;
62-
for (ulInt = -1; ulInt < 0; ulInt *= 2)
85+
for (li = max = 1; li > 0; li *= 2)
86+
max *= 2;
87+
printf("signed long\t\t%li\t%llu\n", li, max - 1);
88+
89+
unsigned long int uli;
90+
91+
for (uli = -1; uli < 0; uli *= 2)
6392
;
64-
printf("unsigned long\t\t%2i\t\t\t%lu\n", 0, ulInt);
93+
printf("unsigned long\t\t%2u\t\t\t%lu\n", 0, uli);
94+
95+
signed long long lli;
6596

66-
signed long long llInt;
67-
signed long long max_llInt = 1;
68-
for (llInt = 1; llInt > 0; llInt *= 2)
69-
max_llInt *= 2;
70-
printf("signed long long\t%lli\t%lli\n", llInt, max_llInt - 1);
97+
for (lli = max = 1; lli > 0; lli *= 2)
98+
max *= 2;
99+
printf("signed long long\t%lli\t%llu\n", lli, max - 1);
71100

72-
unsigned long long ullInt;
73-
for (ullInt = -1; ullInt < 0; ullInt *= 2)
101+
unsigned long long ulli;
102+
for (ulli = -1; ulli < 0; ulli *= 2)
74103
;
75-
printf("unsigned long long\t%2i\t\t\t%llu\n", 0, ullInt);
104+
printf("unsigned long long\t%2i\t\t\t%llu\n", 0, ulli);
76105

77106
printf("\n");
78107

79108
/* floating points */
80109
/* see IEEE 754 standards */
81110
float fltMin, fltMax;
82111
double dblMin, dblMax, mantissa, exponent;
83-
int i;
84112

85113
mantissa = 1.0;
86114
exponent = 1.0;

0 commit comments

Comments
 (0)