diff options
66 files changed, 72 insertions, 266 deletions
diff --git a/ch1/1-03_temp-table-header.c b/ch1/1-03_temp-table-header.c index d0d069c..682712f 100644 --- a/ch1/1-03_temp-table-header.c +++ b/ch1/1-03_temp-table-header.c @@ -11,13 +11,10 @@ int main(void) { float fahr, celsius; int lower, upper, step; - lower = 0; upper = 300; step = 20; - fahr = lower; - printf(" F | C\n"); printf("------------\n"); while (fahr <= upper) { diff --git a/ch1/1-04_celsius_converter.c b/ch1/1-04_celsius_converter.c index a397d9a..921a4df 100644 --- a/ch1/1-04_celsius_converter.c +++ b/ch1/1-04_celsius_converter.c @@ -9,13 +9,10 @@ int main(void) { float fahr, celsius; int lower, upper, step; - lower = 0; upper = 300; step = 20; - celsius = lower; - printf(" C | F\n"); printf("------------\n"); while (celsius <= upper) { diff --git a/ch1/1-05_backwards-table.c b/ch1/1-05_backwards-table.c index a09619a..634c2a9 100644 --- a/ch1/1-05_backwards-table.c +++ b/ch1/1-05_backwards-table.c @@ -11,13 +11,10 @@ int main(void) { float fahr, celsius; int lower, upper, step; - lower = 0; upper = 300; step = 20; - fahr = upper; - printf(" F | C\n"); printf("------------\n"); while (fahr >= 0) { diff --git a/ch1/1-08_space-counter.c b/ch1/1-08_space-counter.c index 0e00176..5faf181 100644 --- a/ch1/1-08_space-counter.c +++ b/ch1/1-08_space-counter.c @@ -11,7 +11,6 @@ int main(void) { char c; int blanks, tabs, nls = 0; - while ((c = getchar()) != EOF) { if (c == ' ') { blanks++; @@ -23,7 +22,6 @@ int main(void) { nls++; } } - printf("%d blanks, %d tabs, and %d newlines.\n", blanks, tabs, nls); return 0; } diff --git a/ch1/1-09_single-spacing.c b/ch1/1-09_single-spacing.c index ab629c5..3c2d80b 100644 --- a/ch1/1-09_single-spacing.c +++ b/ch1/1-09_single-spacing.c @@ -13,7 +13,6 @@ int main(void) { char c; int spaces = 0; - while ((c = getchar()) != EOF) { if (c == ' ') { if (spaces == 0) { @@ -25,6 +24,5 @@ int main(void) { putchar(c); } } - return 0; } diff --git a/ch1/1-10_literal-escapes.c b/ch1/1-10_literal-escapes.c index 903fff4..2131ba0 100644 --- a/ch1/1-10_literal-escapes.c +++ b/ch1/1-10_literal-escapes.c @@ -12,7 +12,6 @@ int main(void) { char c; - while ((c = getchar()) != EOF) { if (c == '\t') { printf("\\t"); @@ -28,6 +27,5 @@ int main(void) { } putchar(c); } - return 0; } diff --git a/ch1/1-11_word-count.c b/ch1/1-11_word-count.c index 1b4056c..85c118e 100644 --- a/ch1/1-11_word-count.c +++ b/ch1/1-11_word-count.c @@ -17,7 +17,6 @@ int main(void) { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; - while ((c = getchar()) != EOF) { nc++; if (c == '\n') { @@ -30,8 +29,6 @@ int main(void) { nw++; } } - printf("%d %d %d\n", nl, nw, nc); - return 0; } diff --git a/ch1/1-12_one-word-per-line.c b/ch1/1-12_one-word-per-line.c index 29a38df..6e70878 100644 --- a/ch1/1-12_one-word-per-line.c +++ b/ch1/1-12_one-word-per-line.c @@ -15,7 +15,6 @@ int main(void) { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; - while ((c = getchar()) != EOF) { nc++; if (c == '\n') { @@ -34,8 +33,6 @@ int main(void) { putchar(c); } } - printf("%d %d %d\n", nl, nw, nc); - return 0; } diff --git a/ch1/1-13_word-length-histogram.c b/ch1/1-13_word-length-histogram.c index b557417..a2ee173 100644 --- a/ch1/1-13_word-length-histogram.c +++ b/ch1/1-13_word-length-histogram.c @@ -33,19 +33,17 @@ int main(void) { * lengths = an array that keeps track of how often words up to x chars long * occur. */ - int state, ltrs, wrds, lines, wlen, i, j; int lengths[MAXWLENGTH]; for (i = 0; i <= MAXWLENGTH; ++i) { lengths[i] = 0; } - ltrs = wrds = wlen = 0; lines = 1; state = OUT; - // Capture input until it ends + /* Capture input until it ends */ while ((i = getchar()) != EOF) { - // If it's whitespace, we've exited a word + /* If it's whitespace, we've exited a word */ if (i == '\n' || i == ' ' || i == '\t') { if (state == IN) { ++wrds; // ...and should increase the count. @@ -54,7 +52,7 @@ int main(void) { if (wlen <= MAXWLENGTH) { ++lengths[wlen]; } - // Reset our word length now. + /* Reset our word length now. */ wlen = 0; } /* If it's a new line, we're still out of a word but need to increment the @@ -70,13 +68,12 @@ int main(void) { /* Everything that's input counts as a letter. */ ++ltrs; } - printf("\nWORD LENGTH FREQUENCY\n "); for (i = 5; i < 80; i += 5) { printf(" %2d", i); } - - printf("\n"); // End the chart heading. + /* End the chart heading. */ + printf("\n"); j = MINWLENGTH; while (j <= MAXWLENGTH) { i = lengths[j]; @@ -84,7 +81,7 @@ int main(void) { printf("%2d | ", j); while (i > 0) { printf("#"); - i = i-1; + i = i - 1; } printf("\n"); } diff --git a/ch1/1-14_character-freq-histogram.c b/ch1/1-14_character-freq-histogram.c index 9ef1d22..b037967 100644 --- a/ch1/1-14_character-freq-histogram.c +++ b/ch1/1-14_character-freq-histogram.c @@ -18,25 +18,21 @@ int main(void) { chars = string containing the characters the program will count lengths = the counts for each character */ - int c; int i = 0; char chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - /* cnum is the number of characters found in the above string */ int cnum = 0; while (chars[i] != '\0') { cnum += 1; ++i; } - /* This array need its members to be initialized to zero. */ int lengths[cnum]; for (i = 0; i <= cnum; ++i) { lengths[i] = 0; } - - // Capture input until it ends + /* Capture input until it ends */ while ((c = getchar()) != EOF) { for (i = 0; i < cnum; ++i) { if (c == chars[i]) { @@ -44,7 +40,7 @@ int main(void) { } } } - // This is ugly and I wish I knew a better way to do it. + /* This is ugly and I wish I knew a better way to do it. */ printf("\nCHARACTER FREQUENCY\n\n 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75\n"); int iter = 0; while (iter <= cnum) { diff --git a/ch1/1-17_over-80.c b/ch1/1-17_over-80.c index 194e450..cbaced6 100644 --- a/ch1/1-17_over-80.c +++ b/ch1/1-17_over-80.c @@ -12,31 +12,25 @@ #define MINLENGTH 80 int main() { - // longline is used as a boolean that tells us if it's a line worth printing + /* longline is used as a boolean that tells us if it's a line worth printing */ int longline = 0; - - // len is simply a character counter, while c is the character itself. + /* len is simply a character counter, while c is the character itself. */ int len, c; char buffer[MINLENGTH]; - while ((c = getchar()) != EOF) { buffer[len] = c; - /* When we meet the end of the line, we need to print the rest of the line, * but only if we're already in a long line. Otherwise, reset our state. */ if (c == '\n') { - if (longline == 1 && len < MINLENGTH - 1) { buffer[len + 1] = '\0'; printf("%-s", buffer); } - len = 0; longline = 0; continue; } - /* When the buffer has filled up, output its contents! */ if (len == MINLENGTH) { buffer[len + 1] = '\0'; @@ -45,12 +39,10 @@ int main() { longline = 1; continue; } - /* If neither of the above cases are caught, increment our counter and fetch more data. */ ++len; } - return 0; } diff --git a/ch1/1-18_strip-blanks.c b/ch1/1-18_strip-blanks.c index 947ce8d..2074129 100644 --- a/ch1/1-18_strip-blanks.c +++ b/ch1/1-18_strip-blanks.c @@ -14,7 +14,6 @@ int get_line(char s[], int lim) { int c, i; - for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) { s[i] = c; } @@ -29,7 +28,6 @@ int get_line(char s[], int lim) { int main(void) { char buffer[MAXLINELENGTH]; int c, i, len; - /* Make sure every line is gone over */ while (len = get_line(buffer, MAXLINELENGTH)) { /* An empty for statement, simply to change the i variable. @@ -37,7 +35,6 @@ int main(void) { * string terminator. */ for (i = len - 2; (i > 0) && (buffer[i] == ' ') || (buffer[i] == '\t'); --i); - /* We've reached the end of the line's actual content. Terminate the line. */ if (i >= 1) { buffer[i + 1] = '\n'; diff --git a/ch1/1-19_reverse-lines.c b/ch1/1-19_reverse-lines.c index f92c6fa..4bb83d9 100644 --- a/ch1/1-19_reverse-lines.c +++ b/ch1/1-19_reverse-lines.c @@ -23,20 +23,16 @@ int get_line(char s[], int limit) { int c, i; - for (i = 0; i < limit && (c = getchar()) != EOF && c != '\n'; ++i) { s[i] = c; } - s[i] = '\0'; - /* If I don't include this check, I can't handle blank lines */ if (c == EOF && i == 0) { return -1; } else { return i; } - } /* Directly reverse a line's contents. */ @@ -44,27 +40,23 @@ void reverse(char input[], int size) { int tmp; int i = 0; size--; - /* If len and i are the same, then there's no reason to proceed */ while (size > i) { - // Store the first character in a temporary spot... + /* Store the first character in a temporary spot... */ tmp = input[i]; - - // ... and swap! + /* ... and swap! */ input[i] = input[size]; input[size] = tmp; - - // Bring our numbers closer together + /* Bring our numbers closer together */ ++i; --size; } } int main(void) { - // An int and a string to store each line's data in + /* An int and a string to store each line's data in */ int line_len; char buffer[MAXLINELENGTH]; - while ((line_len = get_line(buffer, MAXLINELENGTH)) != -1) { reverse(buffer, line_len); printf("%s\n", buffer); diff --git a/ch1/1-21_entab.c b/ch1/1-21_entab.c index 2833520..cba68a6 100644 --- a/ch1/1-21_entab.c +++ b/ch1/1-21_entab.c @@ -19,35 +19,29 @@ int main(void) { while ((c = getchar()) != EOF) { // First thing's first, advance by a column. column++; - if (c == ' ') { /* Add to 'spaces' immediately, we'll decide if it needs to be * output later. */ spaces++; - if (column % TABWIDTH == 0 && spaces > 0) { putchar('\t'); spaces = 0; // No spaces are left when we tab! } - } else { /* Be sure to output any leftover spaces when we come across a * non-space character. This should allow for spaces between words * that don't fall along the tabstop lines. */ - while (spaces > 0) { putchar(' '); spaces--; } - // As usual, reset things on a newline. if (c == '\n') { column = 0; spaces = 0; } - // Now we can output whatever it is. putchar(c); } diff --git a/ch1/1-22_wordwrap.c b/ch1/1-22_wordwrap.c index 8cb940e..5dbeb86 100644 --- a/ch1/1-22_wordwrap.c +++ b/ch1/1-22_wordwrap.c @@ -22,7 +22,6 @@ int main() { int c, tmp, ts; int spaces = 0; int col = 0; - printf("Just type. It'll wrap to %d characters per line.\n", LINEWIDTH); while ((c = getchar()) != EOF) { if (col >= LINEWIDTH) { diff --git a/ch1/1-23_decomment.c b/ch1/1-23_decomment.c index d9c5c90..21823ef 100644 --- a/ch1/1-23_decomment.c +++ b/ch1/1-23_decomment.c @@ -43,7 +43,6 @@ int main() { if (c == '/' && status == OUT) { // Look ahead and store the character that's returned p = getchar(); - if (p == '*') { status = IN_MULTI; } else if (p == '/') { @@ -54,36 +53,27 @@ int main() { continue; } } - // Ignore everything in a single line comment until a newline if (status == IN_SINGLE) { - if (c == '\n') { putchar(c); status = OUT; continue; } - } - // Ignore everything until you reach the end of a multi comment if (status == IN_MULTI && c == '*') { p = getchar(); - if (p == '/') { status = OUT; continue; } - } - // Output everything when we're not in a comment! if (status == OUT || status == IN_STRING) { putchar(c); } - } - /* derpsauce * * diff --git a/ch1/1-24_syntax-checker.c b/ch1/1-24_syntax-checker.c index 444c562..913f859 100644 --- a/ch1/1-24_syntax-checker.c +++ b/ch1/1-24_syntax-checker.c @@ -121,7 +121,6 @@ int main() { } } } - if (escapes > 0) { printf("Invalid escape sequence on line %d!\n", linenr); return 1; @@ -162,7 +161,6 @@ int main() { printf("Unclosed comment at end of file!\n"); return 1; } - printf("All clean.\n"); return 0; } 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; } diff --git a/ch3/3-01_binsearch2.c b/ch3/3-01_binsearch2.c index bcd66c3..6502273 100644 --- a/ch3/3-01_binsearch2.c +++ b/ch3/3-01_binsearch2.c @@ -24,7 +24,6 @@ int binsearch(int x, int v[], int n) { int low, mid, high; - low = 0; high = n - 1; while (low <= high) { @@ -42,10 +41,8 @@ int binsearch(int x, int v[], int n) { int binsearch2(int x, int v[], int n) { int low, mid, high; - low = 0; high = n - 1; - while (low < high) { mid = (low + high) / 2; if (x <= v[mid]) { @@ -54,7 +51,6 @@ int binsearch2(int x, int v[], int n) { low = mid + 1; } } - if (x == v[low]) { return low; } else { diff --git a/ch3/3-02_escape.c b/ch3/3-02_escape.c index 04910ec..25bfa34 100644 --- a/ch3/3-02_escape.c +++ b/ch3/3-02_escape.c @@ -17,7 +17,7 @@ void escape(char s[], char t[]) { int i, j; j = 0; for (i = 0; t[i] != '\0'; i++) { - switch(t[i]) { + switch (t[i]) { case '\n': s[j++] = '\\'; s[j++] = 'n'; diff --git a/ch3/3-03_expand.c b/ch3/3-03_expand.c index b0a4011..f00ba6a 100644 --- a/ch3/3-03_expand.c +++ b/ch3/3-03_expand.c @@ -56,27 +56,19 @@ int main() { char woot[21] = "-a-f0-36-9b-h-"; char derp[21] = "a-28-t"; char dest[61] = ""; - printf("%26s\n", "INPUT | OUTPUT"); printf("%27s\n", "-----------------"); - expand(foo, dest); printf("%16s => %s\n", foo, dest); - expand(bar, dest); printf("%16s => %s\n", bar, dest); - expand(baz, dest); printf("%16s => %s\n", baz, dest); - expand(fob, dest); printf("%16s => %s\n", fob, dest); - expand(woot, dest); printf("%16s => %s\n", woot, dest); - expand(derp, dest); printf("%16s => %s\n", derp, dest); - return 0; } diff --git a/ch3/3-04_itoa2.c b/ch3/3-04_itoa2.c index 6c2fa2f..099b9de 100644 --- a/ch3/3-04_itoa2.c +++ b/ch3/3-04_itoa2.c @@ -21,8 +21,7 @@ void reverse(char s[]) { int c, i, j; - - for (i = 0, j = strlen(s)-1; i < j; i++, j--) { + for (i = 0, j = strlen(s) - 1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; @@ -31,7 +30,6 @@ void reverse(char s[]) { void itoa(int n, char s[]) { int i, sign, min; // Add 'min' for later use - if ((sign = n) < 0) { /* Detect this and add one so it can properly be made positive */ if (n == INT_MIN) { @@ -62,7 +60,6 @@ int main() { int tests[5] = {INT_MIN, INT_MAX, -300, 172, 38478235}; char st[101] = ""; int i; - for (i = 0; i < 5; i++) { itoa(tests[i], st); printf("%12d in string form is %12s\n", tests[i], st); diff --git a/ch3/3-05_itob.c b/ch3/3-05_itob.c index 58066e5..fdb3a15 100644 --- a/ch3/3-05_itob.c +++ b/ch3/3-05_itob.c @@ -16,8 +16,7 @@ void reverse(char s[]) { int c, i, j; - - for (i = 0, j = strlen(s)-1; i < j; i++, j--) { + for (i = 0, j = strlen(s) - 1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; @@ -26,7 +25,6 @@ void reverse(char s[]) { void itob(int n, char s[], unsigned b) { int i, sign, min, rem; // Add 'min' for later use - if ((sign = n) < 0) { /* Detect this and add one so it can properly be made positive */ if (n == INT_MIN) { @@ -76,7 +74,6 @@ int main() { int tests[5] = {INT_MIN, INT_MAX, -300, 172, 38478235}; char st[101] = ""; int i; - for (i = 0; i < 5; i++) { itob(tests[i], st, 16); printf("%12d in string form is %12s\n", tests[i], st); diff --git a/ch3/3-06_itoa3.c b/ch3/3-06_itoa3.c index 3c28b0b..da60092 100644 --- a/ch3/3-06_itoa3.c +++ b/ch3/3-06_itoa3.c @@ -15,8 +15,7 @@ void reverse(char s[]) { int c, i, j; - - for (i = 0, j = strlen(s)-1; i < j; i++, j--) { + for (i = 0, j = strlen(s) - 1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; @@ -25,7 +24,6 @@ void reverse(char s[]) { void itoa(int n, char s[], unsigned fw) { int i, sign, min; // Add 'min' for later use - if ((sign = n) < 0) { /* Detect this and add one so it can properly be made positive */ if (n == INT_MIN) { @@ -58,7 +56,7 @@ void itoa(int n, char s[], unsigned fw) { int main() { int i; char foo[16] = ""; - int tests[5] = {-25, 409689, 8, -1000, 135}; + int tests[5] = { -25, 409689, 8, -1000, 135}; for (i = 0; i < 5; i++) { itoa(tests[i], foo, 5); printf("Pad to 5 spaces! '%s'\n", foo); diff --git a/ch4/4-01_strrindex.c b/ch4/4-01_strrindex.c index b6400c4..7818743 100644 --- a/ch4/4-01_strrindex.c +++ b/ch4/4-01_strrindex.c @@ -30,8 +30,7 @@ void reverse(char s[]) { int c, i, j; - - for (i = 0, j = strlen(s)-1; i < j; i++, j--) { + for (i = 0, j = strlen(s) - 1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; @@ -40,7 +39,6 @@ void reverse(char s[]) { int strindex(char s[], char t[]) { int i, j, k; - for (i = 0; s[i] != '\0'; i++) { for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++) { } @@ -63,7 +61,6 @@ int strrindex(char s[], char t[]) { int o = strindex(s, t) + strlen(t); reverse(s); reverse(t); - // Be sure to return the correct offset with some math :3 return (strlen(s) - o); } @@ -73,7 +70,6 @@ int main() { char needle[MAXLINE] = "ned"; int pos = strrindex(haystack, needle); int i = 0; - /* This is mostly just to provide a handy visual aid */ printf("The phrase %s's rightmost occurrence is %d:\n", needle, pos); printf("%s\n", haystack); diff --git a/ch4/4-02_atof2.c b/ch4/4-02_atof2.c index 69c42fc..a70e881 100644 --- a/ch4/4-02_atof2.c +++ b/ch4/4-02_atof2.c @@ -19,13 +19,10 @@ double atof(char s[]) { double val, power; int i, sign, esign, exp; - /* Skip whitespace */ for (i = 0; isspace(s[i]); i++) { } - sign = (s[i] == '-') ? -1 : 1; - if (s[i] == '+' || s[i] == '-') { i++; } diff --git a/ch4/4-03_calc-modulus.c b/ch4/4-03_calc-modulus.c index 05d5c1b..e4cfb9d 100644 --- a/ch4/4-03_calc-modulus.c +++ b/ch4/4-03_calc-modulus.c @@ -46,7 +46,6 @@ int main() { int type; double op2; char s[MAXOP]; - while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: @@ -109,7 +108,6 @@ double pop(void) { int getop(char s[]) { int i, c; - while ((s[0] = c = getch()) == ' ' || c == '\t') { } s[1] = '\0'; diff --git a/ch4/4-04_top-swap.c b/ch4/4-04_top-swap.c index 2f884d4..5c60e3b 100644 --- a/ch4/4-04_top-swap.c +++ b/ch4/4-04_top-swap.c @@ -48,7 +48,6 @@ int main() { int type; double op2; char s[MAXOP]; - while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: @@ -128,7 +127,6 @@ double pop(void) { int getop(char s[]) { int i = 0; int c, next; - while ((s[0] = c = getch()) == ' ' || c == '\t') { } s[1] = '\0'; @@ -147,7 +145,6 @@ int getop(char s[]) { } else { c = getch(); } - while (isdigit(s[++i] = c)) { c = getch(); } diff --git a/ch4/4-05_math-funcs.c b/ch4/4-05_math-funcs.c index 6c22767..45c350b 100644 --- a/ch4/4-05_math-funcs.c +++ b/ch4/4-05_math-funcs.c @@ -48,7 +48,6 @@ int main() { int type; double op2; char s[MAXOP]; - while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: @@ -143,7 +142,6 @@ double pop(void) { int getop(char s[]) { int i = 0; int c, next; - while ((s[0] = c = getch()) == ' ' || c == '\t') { } s[1] = '\0'; @@ -162,7 +160,6 @@ int getop(char s[]) { } else { c = getch(); } - while (isdigit(s[++i] = c)) { c = getch(); } diff --git a/ch4/4-06_var-support.c b/ch4/4-06_var-support.c index 02b1d09..5df257a 100644 --- a/ch4/4-06_var-support.c +++ b/ch4/4-06_var-support.c @@ -59,7 +59,6 @@ int main() { double op2; char s[MAXOP]; char ltr; - while ((type = getop(s)) != EOF) { if (isalpha(type) && islower(type)) { push(fetch_var(type)); @@ -180,7 +179,6 @@ double pop(void) { int getop(char s[]) { int i = 0; int c, next; - while ((s[0] = c = getch()) == ' ' || c == '\t') { } s[1] = '\0'; @@ -202,7 +200,6 @@ int getop(char s[]) { } else { c = getch(); } - while (isdigit(s[++i] = c)) { c = getch(); } diff --git a/ch4/4-07_ungets.c b/ch4/4-07_ungets.c index 63a7d7f..61e933b 100644 --- a/ch4/4-07_ungets.c +++ b/ch4/4-07_ungets.c @@ -54,7 +54,6 @@ int main() { double op2; char s[MAXOP]; char ltr; - while ((type = getop(s)) != EOF) { if (isalpha(type) && islower(type)) { push(fetch_var(type)); @@ -175,7 +174,6 @@ double pop(void) { int getop(char s[]) { int i = 0; int c, next; - while ((s[0] = c = getch()) == ' ' || c == '\t') { } s[1] = '\0'; @@ -197,7 +195,6 @@ int getop(char s[]) { } else { c = getch(); } - while (isdigit(s[++i] = c)) { c = getch(); } diff --git a/ch4/4-08_one-char.c b/ch4/4-08_one-char.c index 8080453..6a031b5 100644 --- a/ch4/4-08_one-char.c +++ b/ch4/4-08_one-char.c @@ -53,7 +53,6 @@ int main() { double op2; char s[MAXOP]; char ltr; - while ((type = getop(s)) != EOF) { if (isalpha(type) && islower(type)) { push(fetch_var(type)); @@ -174,7 +173,6 @@ double pop(void) { int getop(char s[]) { int i = 0; int c, next; - while ((s[0] = c = getch()) == ' ' || c == '\t') { } s[1] = '\0'; @@ -196,7 +194,6 @@ int getop(char s[]) { } else { c = getch(); } - while (isdigit(s[++i] = c)) { c = getch(); } diff --git a/ch4/4-09_pushed-eof.c b/ch4/4-09_pushed-eof.c index f130992..53a1af4 100644 --- a/ch4/4-09_pushed-eof.c +++ b/ch4/4-09_pushed-eof.c @@ -53,7 +53,6 @@ int main() { double op2; char s[MAXOP]; char ltr; - ungetch(EOF); while ((type = getop(s)) != EOF) { if (isalpha(type) && islower(type)) { @@ -175,7 +174,6 @@ double pop(void) { int getop(char s[]) { int i = 0; int c, next; - while ((s[0] = c = getch()) == ' ' || c == '\t') { } s[1] = '\0'; @@ -197,7 +195,6 @@ int getop(char s[]) { } else { c = getch(); } - while (isdigit(s[++i] = c)) { c = getch(); } diff --git a/ch4/4-10_getline-calc.c b/ch4/4-10_getline-calc.c index 499e71a..a801f0c 100644 --- a/ch4/4-10_getline-calc.c +++ b/ch4/4-10_getline-calc.c @@ -63,7 +63,6 @@ int main() { double op2; char s[MAXOP]; char ltr; - while (mygetline(buf, BUFSIZE) != 0) { bufp = 0; while ((type = getop(s)) != '\0') { @@ -188,7 +187,6 @@ double pop(void) { int getop(char s[]) { int i, c; - while ((s[0] = c = buf[bufp++]) == ' ' || c == '\t'); if (c >= 'a' && c <= 'z') { return c; @@ -210,7 +208,6 @@ int getop(char s[]) { } else { s[i] = c; } - if (isdigit(c)) { while (isdigit(s[++i] = c = buf[bufp++])); } @@ -264,7 +261,6 @@ double fetch_last(void) { int mygetline(char s[], int lim) { int c, i = 0; - while (--lim > 0 && (c = getchar()) != EOF && c != '\n') { s[i++] = c; } diff --git a/ch4/4-11_getop-v2.c b/ch4/4-11_getop-v2.c index 6c2349b..dca0dbb 100644 --- a/ch4/4-11_getop-v2.c +++ b/ch4/4-11_getop-v2.c @@ -51,7 +51,6 @@ int main() { double op2; char s[MAXOP]; char ltr; - while ((type = getop(s)) != EOF) { if (isalpha(type) && islower(type)) { push(fetch_var(type)); @@ -173,7 +172,6 @@ int getop(char s[]) { int i = 0; int c, next; static int buf = EOF; - if (buf != EOF && buf != ' ' && buf != '\t' && !isdigit(buf) && buf != '.' && (buf < 'a' || buf > 'z')) { c = buf; buf = EOF; @@ -205,7 +203,6 @@ int getop(char s[]) { } else { c = getch(); } - while (isdigit(s[++i] = c)) { c = getch(); } diff --git a/ch4/4-12_recursive-itoa.c b/ch4/4-12_recursive-itoa.c index f2ebdcf..1a6afc4 100644 --- a/ch4/4-12_recursive-itoa.c +++ b/ch4/4-12_recursive-itoa.c @@ -25,24 +25,20 @@ void reverse(char[]); int main(void) { char foo[40] = ""; - itoa(829048, foo); printf("%s\n", foo); itoa(-4021, foo); printf("%s\n", foo); - return 0; } void itoa(int num, char target[]) { static int i = 0; static int neg = 0; - if (num < 0) { neg = 1; num = -num; } - if (num /= 10 > 0) { target[i++] = (num % 10) + '0'; num /= 10; @@ -63,8 +59,7 @@ void itoa(int num, char target[]) { void reverse(char s[]) { int c, i, j; - - for (i = 0, j = strlen(s)-1; i < j; i++, j--) { + for (i = 0, j = strlen(s) - 1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; diff --git a/ch4/4-13_recursive-reverse.c b/ch4/4-13_recursive-reverse.c index 85a5b91..6fffad3 100644 --- a/ch4/4-13_recursive-reverse.c +++ b/ch4/4-13_recursive-reverse.c @@ -29,11 +29,9 @@ void reverse(char s[]) { static int i = 0; static int j = 0; int c; - if (j == 0) { j = strlen(s) - 1; } - if (i < j) { c = s[i]; s[i] = s[j]; diff --git a/ch4/4-14_swap-macro.c b/ch4/4-14_swap-macro.c index ac2f102..7e794d4 100644 --- a/ch4/4-14_swap-macro.c +++ b/ch4/4-14_swap-macro.c @@ -22,7 +22,6 @@ int main() { int a = 3; int b = 7; - printf("a is %d, b is %d\n", a, b); swap(int, a, b); printf("Now swap, and... a is %d and b is %d!\n", a, b); diff --git a/ch5/5-01_getint-fixed.c b/ch5/5-01_getint-fixed.c index b013f20..641251f 100644 --- a/ch5/5-01_getint-fixed.c +++ b/ch5/5-01_getint-fixed.c @@ -29,7 +29,6 @@ int main() { int getint(int *pn) { int c, sign; - while (isspace(c = getch())) { } if (!isdigit(c) && c != EOF && c != '+' && c != '-') { diff --git a/ch5/5-02_getfloat.c b/ch5/5-02_getfloat.c index e6f3347..b125976 100644 --- a/ch5/5-02_getfloat.c +++ b/ch5/5-02_getfloat.c @@ -32,7 +32,6 @@ int main() { int getfloat(double *pn) { int c, sign, places; - while (isspace(c = getch())) { } if (!isdigit(c) && c != EOF && c != '+' && c != '-') { diff --git a/ch5/5-06_pointer-funcs.c b/ch5/5-06_pointer-funcs.c index 7947ba6..a64a0fe 100644 --- a/ch5/5-06_pointer-funcs.c +++ b/ch5/5-06_pointer-funcs.c @@ -60,7 +60,7 @@ void reverse(char *s) { int get_line(char *s, int lim) { int c, i; - for (i = 0; i < lim - 1 && (c=getchar()) != EOF && c != '\n'; ++i) { + for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) { *s++ = c; } if (c == '\n') { diff --git a/ch5/5-08_error-checking.c b/ch5/5-08_error-checking.c index 61c15c7..b1bc5e7 100644 --- a/ch5/5-08_error-checking.c +++ b/ch5/5-08_error-checking.c @@ -19,7 +19,6 @@ int main() { int m, d, doy; m = d = 0; doy = 0; - printf("Calling day_of_year(1973, 10, 32)\n> "); if ((doy = day_of_year(1973, 10, 32)) > 0) { printf("Oct 12nd, 1973 is day %d\n", doy); @@ -52,7 +51,6 @@ int main() { int day_of_year(int year, int month, int day) { int i, leap; leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; - if (month < 1 || month > 12) { return -1; } @@ -68,7 +66,6 @@ int day_of_year(int year, int month, int day) { void month_day(int year, int yearday, int *pmonth, int *pday) { int i, leap; leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; - // Check for edge case if (yearday > 365 + leap || yearday < 1) { *pmonth = 0; // Set to zero so the error is obvious diff --git a/ch5/5-09_yearday-pointers.c b/ch5/5-09_yearday-pointers.c index 7a28f88..df7197d 100644 --- a/ch5/5-09_yearday-pointers.c +++ b/ch5/5-09_yearday-pointers.c @@ -22,7 +22,6 @@ int main() { }; m = d = 0; doy = 0; - // It said nothing about avoiding indexing in main()! for (i = 0; i < 3; i++) { doy = day_of_year(tvals[i][0], tvals[i][1], tvals[i][2]); @@ -40,7 +39,6 @@ int main() { int day_of_year(int year, int month, int day) { int i, leap; leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; - if (month < 1 || month > 12) { return -1; } @@ -50,7 +48,7 @@ int day_of_year(int year, int month, int day) { } for (i = 1; i < month; i++) { // here, too - day += *(daytab + i) + ((i ==2) ? leap : 0); + day += *(daytab + i) + ((i == 2) ? leap : 0); } return day; } @@ -58,7 +56,6 @@ int day_of_year(int year, int month, int day) { void month_day(int year, int yearday, int *pmonth, int *pday) { int i, leap; leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; - if (yearday > 365 + leap || yearday < 1) { *pmonth = 0; *pday = 0; diff --git a/ch5/5-10_expr.c b/ch5/5-10_expr.c index cc88838..acaabca 100644 --- a/ch5/5-10_expr.c +++ b/ch5/5-10_expr.c @@ -27,7 +27,7 @@ double pop(void); * calculator. Note that for most shells, you'll need to escape the * multiplication symbol (*) or it will expand to every file in the current * directory; NOT what you want in this situation. :) - * + * * So if you want 6 × 8 for example, you'll need to type `6 8 \*` */ int main(int argc, char *argv[]) { @@ -35,7 +35,7 @@ int main(int argc, char *argv[]) { double op2; for (argp = 1; argp < argc; argp++) { int argtype = opt_type(argv[argp]); - switch(argtype) { + switch (argtype) { case '*': push(pop() * pop()); break; @@ -80,7 +80,7 @@ double pop(void) { int opt_type(char op[]) { int i = 0; - switch(op[i]) { + switch (op[i]) { case '*': return '*'; case '+': @@ -88,12 +88,11 @@ int opt_type(char op[]) { case '/': return '/'; case '-': - if (op[i+1] == '\0') { + if (op[i + 1] == '\0') { return '-'; } break; } - while (isdigit(op[i])) { i++; } diff --git a/ch5/5-11_detab-remixed.c b/ch5/5-11_detab-remixed.c index 43bffb4..457d465 100644 --- a/ch5/5-11_detab-remixed.c +++ b/ch5/5-11_detab-remixed.c @@ -12,19 +12,17 @@ int main(int argc, char *argv[]) { int column, c, tabnum, stop; column = 0; - if (argc > 1) { tabnum = 1; stop = atoi(argv[tabnum]); } else { tabnum = 0; } - while ((c = getchar()) != EOF) { if (c == '\t') { if (argc > 1) { // advance the argument if we're ahead of the last one - if (column > stop && tabnum < (argc -1)) { + if (column > stop && tabnum < (argc - 1)) { stop = atoi(argv[++tabnum]); } // insert our spaces up to the tabstop @@ -56,6 +54,5 @@ int main(int argc, char *argv[]) { column++; } } - return 0; } diff --git a/ch5/5-11_entab-remixed.c b/ch5/5-11_entab-remixed.c index 8f6f7d4..d517365 100644 --- a/ch5/5-11_entab-remixed.c +++ b/ch5/5-11_entab-remixed.c @@ -21,17 +21,14 @@ int main(int argc, char *argv[]) { int column, c, spaces, tabnum, stop; spaces = column = 0; - if (argc > 1) { tabnum = 1; stop = atoi(argv[tabnum]); } else { tabnum = 0; } - while ((c = getchar()) != EOF) { column++; - if (c == ' ') { spaces++; if (argc > 1) { @@ -43,11 +40,11 @@ int main(int argc, char *argv[]) { putchar('\t'); spaces = 0; } - } else if (column > stop && tabnum < (argc -1)) { + } else if (column > stop && tabnum < (argc - 1)) { stop = atoi(argv[++tabnum]); } } else { - // we need to do default tabstopping + // we need to do default tabstopping if (column % TABWIDTH == 0 && spaces > 1) { putchar('\t'); spaces = 0; @@ -71,6 +68,5 @@ int main(int argc, char *argv[]) { putchar(c); } } - return 0; } diff --git a/ch5/5-12_detab-spec.c b/ch5/5-12_detab-spec.c index ba22619..84f81d5 100644 --- a/ch5/5-12_detab-spec.c +++ b/ch5/5-12_detab-spec.c @@ -16,7 +16,6 @@ int main(int argc, char *argv[]) { int tabevery = (-1); char arg[4]; column = 0; - if (argc > 1) { int i = 1; int j = 0; @@ -47,10 +46,8 @@ int main(int argc, char *argv[]) { if (tabevery == -1) { tabevery = DEFAULTTAB; } - while ((c = getchar()) != EOF) { column++; - if (c == '\t') { /* No point in processing our tabs until we're after the offset */ if (column > tabstart) { @@ -70,6 +67,5 @@ int main(int argc, char *argv[]) { putchar(c); } } - return 0; } diff --git a/ch5/5-12_entab-spec.c b/ch5/5-12_entab-spec.c index c1f3f6b..d551f66 100644 --- a/ch5/5-12_entab-spec.c +++ b/ch5/5-12_entab-spec.c @@ -17,7 +17,6 @@ int main(int argc, char *argv[]) { int tabevery = (-1); char arg[4]; spaces = column = 0; - if (argc > 1) { int i = 1; int j = 0; @@ -48,10 +47,8 @@ int main(int argc, char *argv[]) { if (tabevery == -1) { tabevery = DEFAULTTAB; } - while ((c = getchar()) != EOF) { column++; - if (c == ' ') { /* No point in starting our tabs until we're after the offset */ if (column > tabstart) { @@ -77,6 +74,5 @@ int main(int argc, char *argv[]) { putchar(c); } } - return 0; } diff --git a/ch5/5-13_tail.c b/ch5/5-13_tail.c index 3a8ea30..642c7c5 100644 --- a/ch5/5-13_tail.c +++ b/ch5/5-13_tail.c @@ -54,7 +54,7 @@ int readlines(char *lineptr[], int maxlines) { if (nlines >= maxlines || (p = alloc(len)) == NULL) { return -1; } else { - line[len-1] = '\0'; + line[len - 1] = '\0'; strcpy(p, line); lineptr[nlines++] = p; } @@ -90,7 +90,6 @@ void parse_args(int argc, char *argv[]) { int main(int argc, char *argv[]) { int nlines; parse_args(argc, argv); - if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { writelines(lineptr, nlines - numlines, nlines); return 0; diff --git a/ch5/5-14_rsort.c b/ch5/5-14_rsort.c index b39a20c..5b19e6d 100644 --- a/ch5/5-14_rsort.c +++ b/ch5/5-14_rsort.c @@ -29,7 +29,6 @@ int numcmp(const char *, const char *); void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const char *)) { int i, last; void swap(void *v[], int, int); - if (left >= right) { return; } @@ -47,7 +46,6 @@ void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const ch int numcmp(const char *s1, const char *s2) { double v1, v2; - v1 = atof(s1); v2 = atof(s2); if (v1 < v2) { @@ -61,7 +59,6 @@ int numcmp(const char *s1, const char *s2) { void swap(void *v[], int i, int j) { void *temp; - temp = v[i]; v[i] = v[j]; v[j] = temp; @@ -75,7 +72,7 @@ int readlines(char *lineptr[], int maxlines) { if (nlines >= maxlines || (p = alloc(len)) == NULL) { return -1; } else { - line[len-1] = '\0'; + line[len - 1] = '\0'; strcpy(p, line); lineptr[nlines++] = p; } @@ -123,17 +120,16 @@ char *alloc(int n) { } /* sort input lines */ -int main (int argc, char *argv[]) { +int main(int argc, char *argv[]) { int nlines; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ int reverse = 0; /* 1 if reverse sort */ - if (argc > 1) { int i, j; for (i = 1, j = 0; --argc; i++) { if (argv[i][j++] == '-') { while (argv[i][j] != '\0') { - switch(argv[i][j]) { + switch (argv[i][j]) { case 'n': numeric = 1; break; diff --git a/ch5/5-15_sort-fold.c b/ch5/5-15_sort-fold.c index 90b567b..90c658f 100644 --- a/ch5/5-15_sort-fold.c +++ b/ch5/5-15_sort-fold.c @@ -31,7 +31,6 @@ int istrcmp(const char *, const char *); void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const char *)) { int i, last; void swap(void *v[], int, int); - if (left >= right) { return; } @@ -49,7 +48,6 @@ void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const ch int numcmp(const char *s1, const char *s2) { double v1, v2; - v1 = atof(s1); v2 = atof(s2); if (v1 < v2) { @@ -63,7 +61,6 @@ int numcmp(const char *s1, const char *s2) { void swap(void *v[], int i, int j) { void *temp; - temp = v[i]; v[i] = v[j]; v[j] = temp; @@ -77,7 +74,7 @@ int readlines(char *lineptr[], int maxlines) { if (nlines >= maxlines || (p = alloc(len)) == NULL) { return -1; } else { - line[len-1] = '\0'; + line[len - 1] = '\0'; strcpy(p, line); lineptr[nlines++] = p; } @@ -104,7 +101,7 @@ void reverse_set(char *lineptr[], int nlines) { /* Ignore character case, then send results to qsort */ int istrcmp(const char *s1, const char *s2) { - for ( ; tolower(*s1) == tolower(*s2); s1++, s2++) { + for (; tolower(*s1) == tolower(*s2); s1++, s2++) { if (*s1 == '\0') { return 0; } @@ -135,18 +132,17 @@ char *alloc(int n) { } /* sort input lines */ -int main (int argc, char *argv[]) { +int main(int argc, char *argv[]) { int nlines; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ int reverse = 0; /* 1 if reverse sort */ int fold = 0; /* 1 if folding upper and lower case */ - if (argc > 1) { int i, j; for (i = 1, j = 0; --argc; i++) { if (argv[i][j++] == '-') { while (argv[i][j] != '\0') { - switch(argv[i][j]) { + switch (argv[i][j]) { case 'n': numeric = 1; break; diff --git a/ch5/5-16_dir-order.c b/ch5/5-16_dir-order.c index 4610ea6..2952e23 100644 --- a/ch5/5-16_dir-order.c +++ b/ch5/5-16_dir-order.c @@ -49,7 +49,6 @@ int mygetline(char *line, int lim) { void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const char *)) { int i, last; void swap(void *v[], int, int); - if (left >= right) { return; } @@ -67,7 +66,6 @@ void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const ch int numcmp(const char *s1, const char *s2) { double v1, v2; - v1 = atof(s1); v2 = atof(s2); if (v1 < v2) { @@ -81,7 +79,6 @@ int numcmp(const char *s1, const char *s2) { void swap(void *v[], int i, int j) { void *temp; - temp = v[i]; v[i] = v[j]; v[j] = temp; @@ -95,7 +92,7 @@ int readlines(char *lineptr[], int maxlines) { if (nlines >= maxlines || (p = alloc(len)) == NULL) { return -1; } else { - line[len-1] = '\0'; + line[len - 1] = '\0'; strcpy(p, line); lineptr[nlines++] = p; } @@ -160,13 +157,12 @@ char *alloc(int n) { /* sort input lines */ int main(int argc, char *argv[]) { int nlines; /* number of input lines read */ - if (argc > 1) { int i, j; for (i = 1, j = 0; --argc; i++) { if (argv[i][j++] == '-') { while (argv[i][j] != '\0') { - switch(argv[i][j]) { + switch (argv[i][j]) { case 'n': numeric = 1; break; diff --git a/ch5/5-17_field-sort.c b/ch5/5-17_field-sort.c index c086053..078b347 100644 --- a/ch5/5-17_field-sort.c +++ b/ch5/5-17_field-sort.c @@ -64,7 +64,6 @@ int mygetline(char *line, int lim) { void my_qsort(void *v[], int left, int right, int (*comp)(const char *, const char *)) { int i, last; void swap(void *v[], int, int); - if (left >= right) { return; } @@ -108,7 +107,7 @@ int readlines(char *lineptr[], int maxlines) { if (nlines >= maxlines || (p = alloc(len)) == NULL) { return -1; } else { - line[len-1] = '\0'; + line[len - 1] = '\0'; strcpy(p, line); lineptr[nlines++] = p; } @@ -170,8 +169,8 @@ int field_start(const char *s, int fieldnum) { if (s[i] == DELIMITER) { fieldnum--; if (fieldnum == 0) { - if (s[i+1] != '\0') { - return i+1; + if (s[i + 1] != '\0') { + return i + 1; } } } @@ -227,7 +226,7 @@ int main(int argc, char *argv[]) { for (i = 1, j = 0; --argc; i++) { if (argv[i][j++] == '-') { while (argv[i][j] != '\0') { - switch(argv[i][j]) { + switch (argv[i][j]) { case 'n': numeric = 1; break; diff --git a/ch5/5-18_dcl-error-recovery.c b/ch5/5-18_dcl-error-recovery.c index 9a22764..03a7e3c 100644 --- a/ch5/5-18_dcl-error-recovery.c +++ b/ch5/5-18_dcl-error-recovery.c @@ -80,7 +80,7 @@ int gettoken(void) { bufp++; } if (buf[bufp] == '/') { - if (buf[bufp+1] == '/' || buf[bufp+1] == '*') { + if (buf[bufp + 1] == '/' || buf[bufp + 1] == '*') { while (buf[bufp] != '\n') { bufp++; } @@ -130,7 +130,7 @@ int gettoken(void) { */ int get_line(char *s, int lim) { int c, i; - for (i = 0; i < lim - 1 && (c=getchar()) != EOF && c != '\n'; ++i) { + for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) { *s++ = c; } if (c == '\n') { diff --git a/ch5/5-19_undcl-no-redundant-parens.c b/ch5/5-19_undcl-no-redundant-parens.c index 4af4314..44d5f38 100644 --- a/ch5/5-19_undcl-no-redundant-parens.c +++ b/ch5/5-19_undcl-no-redundant-parens.c @@ -44,12 +44,12 @@ int gettoken(void) { return tokentype = '('; } } else if (c == '[') { - for (*p++ = c; (*p++ = getch()) != ']'; ) { + for (*p++ = c; (*p++ = getch()) != ']';) { } *p = '\0'; return tokentype = BRACKETS; } else if (isalpha(c)) { - for (*p++ = c; isalnum(c = getch()); ) { + for (*p++ = c; isalnum(c = getch());) { *p++ = c; } *p = '\0'; diff --git a/ch5/5-20_expanded-dcl.c b/ch5/5-20_expanded-dcl.c index d7d4c5c..a30fa3e 100644 --- a/ch5/5-20_expanded-dcl.c +++ b/ch5/5-20_expanded-dcl.c @@ -107,7 +107,7 @@ int gettoken(void) { bufp++; } if (buf[bufp] == '/') { - if (buf[bufp+1] == '/' || buf[bufp+1] == '*') { + if (buf[bufp + 1] == '/' || buf[bufp + 1] == '*') { while (buf[bufp] != '\n') { bufp++; } @@ -167,7 +167,7 @@ int gettoken(void) { */ int get_line(char *s, int lim) { int c, i; - for (i = 0; i < lim - 1 && (c=getchar()) != EOF && c != '\n'; ++i) { + for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) { *s++ = c; } if (c == '\n') { diff --git a/ch6/6-01_getword-enhanced.c b/ch6/6-01_getword-enhanced.c index c040d1e..7e2777e 100644 --- a/ch6/6-01_getword-enhanced.c +++ b/ch6/6-01_getword-enhanced.c @@ -159,7 +159,7 @@ int getword(char *word, int lim) { *w = '\0'; return c; } - for ( ; --lim > 0; w++) { + for (; --lim > 0; w++) { if (!isalnum(*w = getch()) && *w != '_') { ungetch(*w); break; diff --git a/ch6/6-02_common-prefix-printer.c b/ch6/6-02_common-prefix-printer.c index fedb389..f5314d7 100644 --- a/ch6/6-02_common-prefix-printer.c +++ b/ch6/6-02_common-prefix-printer.c @@ -53,7 +53,7 @@ struct tnode *addtree(struct tnode *, char *); void treeprint(struct tnode *, int); struct tnode *talloc(void); char *mystrdup(char *); -int check_prefix(char *, char*, int); +int check_prefix(char *, char *, int); int main(int argc, char *argv[]) { switch (argc) { @@ -130,7 +130,7 @@ int getword(char *word, int lim) { *w = '\0'; return c; } - for ( ; --lim > 0; w++) { + for (; --lim > 0; w++) { if (!isalnum(*w = getch()) && *w != '_') { ungetch(*w); break; @@ -146,7 +146,7 @@ struct key *binsearch(char *word, struct key *tab, int n) { struct key *high = &tab[n]; struct key *mid; while (low < high) { - mid = low + (high-low) / 2; + mid = low + (high - low) / 2; if ((cond = strcmp(word, mid->word)) < 0) { high = mid; } else if (cond > 0) { @@ -231,7 +231,7 @@ struct tnode *talloc(void) { char *mystrdup(char *s) { char *p; - p = (char *) malloc(strlen(s)+1); + p = (char *) malloc(strlen(s) + 1); if (p != NULL) { strcpy(p , s); } diff --git a/ch6/6-03_line_location.c b/ch6/6-03_line_location.c index 9a22c81..52c1782 100644 --- a/ch6/6-03_line_location.c +++ b/ch6/6-03_line_location.c @@ -93,7 +93,7 @@ int getword(char *word, int lim) { if (c != EOF) { *w++ = c; } - for ( ; --lim > 0; w++) { + for (; --lim > 0; w++) { if (!isalpha(*w = getch())) { ungetch(*w); break; @@ -182,7 +182,7 @@ struct tlist *lalloc(void) { char *mystrdup(char *s) { char *p; - p = (char *) malloc(strlen(s)+1); + p = (char *) malloc(strlen(s) + 1); if (p != NULL) { strcpy(p , s); } diff --git a/ch6/6-04_top-words.c b/ch6/6-04_top-words.c index 796dac1..784a950 100644 --- a/ch6/6-04_top-words.c +++ b/ch6/6-04_top-words.c @@ -114,7 +114,7 @@ int getword(char *word, int lim) { if (c != EOF) { *w++ = c; } - for ( ; --lim > 0; w++) { + for (; --lim > 0; w++) { if (!isalpha(*w = getch())) { ungetch(*w); break; @@ -169,7 +169,7 @@ struct tnode *talloc(void) { char *mystrdup(char *s) { char *p; - p = (char *) malloc(strlen(s)+1); + p = (char *) malloc(strlen(s) + 1); if (p != NULL) { strcpy(p , s); } diff --git a/ch6/6-05_undef.c b/ch6/6-05_undef.c index d65f435..40ac87e 100644 --- a/ch6/6-05_undef.c +++ b/ch6/6-05_undef.c @@ -79,7 +79,7 @@ struct nlist *install(char *name, char *defn) { np->next = hashtab[hashval]; hashtab[hashval] = np; } else { - free ((void *) np->defn); + free((void *) np->defn); } if ((np->defn = strdup(defn)) == NULL) { return NULL; diff --git a/ch6/6-06_define-processor.c b/ch6/6-06_define-processor.c index d26ddbc..e24f173 100644 --- a/ch6/6-06_define-processor.c +++ b/ch6/6-06_define-processor.c @@ -115,7 +115,7 @@ struct nlist *install(char *name, char *defn) { np->next = hashtab[hashval]; hashtab[hashval] = np; } else { - free ((void *) np->defn); + free((void *) np->defn); } if ((np->defn = strdup(defn)) == NULL) { return NULL; @@ -188,7 +188,7 @@ int getword(char *word, int lim) { *w = '\0'; return c; } - for ( ; --lim > 0; w++) { + for (; --lim > 0; w++) { if (!isalnum(*w = getch())) { ungetch(*w); break; diff --git a/ch7/7-01_upper-lower.c b/ch7/7-01_upper-lower.c index 2f641c1..047c83c 100644 --- a/ch7/7-01_upper-lower.c +++ b/ch7/7-01_upper-lower.c @@ -26,12 +26,12 @@ int main(int argc, char *argv[]) { plow = strstr(argv[0], "lower"); pupp = strstr(argv[0], "upper"); if (plow != NULL) { - lpos = (int) (plow - argv[0]); + lpos = (int)(plow - argv[0]); } else { lpos = -1; } if (pupp != NULL) { - upos = (int) (pupp - argv[0]); + upos = (int)(pupp - argv[0]); } else { upos = -1; } |