aboutsummaryrefslogtreecommitdiff
path: root/ch2
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--ch2/2-01_limits.c21
-rw-r--r--ch2/2-03_hex-to-int.c7
-rw-r--r--ch2/2-04_squeeze-v2.c13
-rw-r--r--ch2/2-05_any-func.c3
4 files changed, 8 insertions, 36 deletions
diff --git a/ch2/2-01_limits.c b/ch2/2-01_limits.c
index 06d7450..2e8dff4 100644
--- a/ch2/2-01_limits.c
+++ b/ch2/2-01_limits.c
@@ -31,96 +31,75 @@ int main() {
float fl, fla, fll;
double db, dba, dbl;
long double ldb, ldba, ldbl;
-
printf("Let's compute the minimums and maximums of each type!\n");
uc = sc = us = ss = ui = si = ul = sl = sll = ull = 0;
fl = fla = fll = db = dba = dbl = ldb = ldba = ldbl = 0.0;
-
/* Characters */
-
sc++;
while (sc * 2 > sc) {
sc = sc * 2 + 1;
}
-
printf("`signed char` maximum: %d\n", sc);
sc++;
printf("`signed char` minimum: %d\n", sc);
uc--;
printf("`unsigned char` maximum: %u\n", uc);
-
/* Short ints */
-
ss++;
while (ss * 2 > ss) {
ss = ss * 2 + 1;
}
-
printf("`signed short` maximum: %d\n", ss);
ss++;
printf("`signed short` minimum: %d\n", ss);
us--;
printf("`unsigned short` maximum: %u\n", us);
-
/* Integers */
-
si++;
while (si * 2 > si) {
si = si * 2 + 1;
}
-
printf("`signed int` maximum: %d\n", si);
si++;
printf("`signed int` minimum: %d\n", si);
ui--;
printf("`unsigned int` maximum: %u\n", ui);
-
/* Long ints */
-
sl++;
while (sl * 2 > sl) {
sl = sl * 2 + 1;
}
-
printf("`signed long` maximum: %li\n", sl);
sl++;
printf("`signed long` minimum: %li\n", sl);
ul--;
printf("`unsigned long` maximum: %lu\n", ul);
-
/* Long long ints */
-
sll++;
while (sll * 2 > sll) {
sll = sll * 2 + 1;
}
-
printf("`signed long long` maximum: %lli\n", sll);
sll++;
printf("`signed long long` minimum: %lli\n", sll);
ull--;
printf("`unsigned long long` maximum: %llu\n", ull);
-
/* Floats */
while (fl == 0.0) {
fll = fla;
fla = fla + 1111e28;
fl = (fl + fla) - fla;
}
-
printf("`float` maximum: %e\n", fll);
printf("`float` minimum: %e\n", 0 - fll);
-
/* Doubles */
while (db == 0.0) {
dbl = dba;
dba = dba + 1111e297;
db = (db + dba) - dba;
}
-
printf("`double` maximum: %e\n", dbl);
printf("`double` minimum: %e\n", 0 - dbl);
-
printf("\nNow, let's cheat and use the helpful headers!\n");
printf("`char`s go from %d to %d (unsigned, up to %u)\n", SCHAR_MIN, SCHAR_MAX, UCHAR_MAX);
printf("`short`s go from %d to %d (unsigned, up to %u)\n", SHRT_MIN, SHRT_MAX, USHRT_MAX);
diff --git a/ch2/2-03_hex-to-int.c b/ch2/2-03_hex-to-int.c
index 566bd75..4985bc5 100644
--- a/ch2/2-03_hex-to-int.c
+++ b/ch2/2-03_hex-to-int.c
@@ -25,16 +25,15 @@
int htoi(char s[]) {
int i, val;
-
for (i = val = 0; isxdigit(s[i]) || toupper(s[i]) == 'X'; ++i) {
if (toupper(s[i]) == 'X') {
continue;
}
if (s[i] > '9') {
val = 16 * val + (toupper(s[i]) - '7');
- // The 7 is because 'A' is 7 higher than '9' in ASCII and thus only needs
- // to be knocked down by that much to fall in line with the normal integer
- // conversion
+ /* The 7 is because 'A' is 7 higher than '9' in ASCII and thus only needs */
+ /* to be knocked down by that much to fall in line with the normal integer */
+ /* conversion */
} else {
val = 16 * val + (s[i] - '0');
}
diff --git a/ch2/2-04_squeeze-v2.c b/ch2/2-04_squeeze-v2.c
index 2c3b56a..7a49585 100644
--- a/ch2/2-04_squeeze-v2.c
+++ b/ch2/2-04_squeeze-v2.c
@@ -16,13 +16,12 @@
*
*/
-// It'd be better to make this return a pointer (or string), but the book
-// hasn't covered it yet!
+/* It'd be better to make this return a pointer (or string), but the book */
+/* hasn't covered it yet! */
void squeeze(char s1[], char s2[]) {
int i, j, k, match;
-
for (i = j = 0; s1[i] != '\0'; i++) {
- // I don't see a way to do this without a flag
+ /* I don't see a way to do this without a flag */
match = 0;
for (k = 0; s2[k] != '\0'; k++) {
if (s1[i] == s2[k]) {
@@ -30,22 +29,18 @@ void squeeze(char s1[], char s2[]) {
break;
}
}
- // check our flag. if there wasn't a match, j needs to match i's value
+ /* check our flag. if there wasn't a match, j needs to match i's value */
if (!match) {
s1[j++] = s1[i];
}
}
-
s1[j] = '\0';
}
int main() {
char foo[16] = "foobarbaz";
char bar[16] = "boz";
-
squeeze(foo, bar);
-
printf("%s\n", foo); // Should read "fara"
-
return 0;
}
diff --git a/ch2/2-05_any-func.c b/ch2/2-05_any-func.c
index c515fab..6e09de1 100644
--- a/ch2/2-05_any-func.c
+++ b/ch2/2-05_any-func.c
@@ -33,12 +33,11 @@ int main() {
char foo[80] = "argle bargle";
char bar[80] = "toobz";
int result;
-
if ((result = any(foo, bar)) != -1) {
printf("Found a match at position %d!\n", result);
} else {
printf("No match found.\n");
}
- // Should return a match at index 6.
+ /* Should return a match at index 6. */
return 0;
}