4 ms·
§ 1.5.3 Line Counting #include <stdio.h> /* count lines in input */ main() { int c, nl; nl = 0; while ((c = getchar()) != EOF)
by susam 8y ago
§ 1.5.3 Line Counting
#include <stdio.h>
/* count lines in input */
main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}
§ 1.5.4 Word Counting
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
/* count lines, words, and characters in input */
main()
{
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
Also, see § 1.6 Arrays, § 2.8 Increment and Decrement Operators, etc.
By the way, I would always use brace while working on a team project where there are other developers involved. But this is a one-time, personal, and hobby project where I am the only developer and I prefer to choose the K&R style when nobody else is involved.
- tom_mellior 8y agoWow, that was quick, thanks. Though the first example is only two levels deep and also less complex than your code in question, which has a loop in the else part of an if in a loop... The second example uses braces (as it must), so it's not an example of what I was asking for.
- susam 8y agoOkay, I was counting the nesting depth incorrectly. Here are a few more examples: § 3.8 Goto and labels found = 0; for (i = 0; i < n && !found; i++) for (j = 0; j < m && !found; j++) if (a[i] == b[j]) found = 1; if (found) /* got one: a[i-1] == b[j-1] */ ... else /* didn't find any common element */ § 6.3 Arrays of Structures /* count C keywords */ main() { int n; char word[MAXWORD]; while (getword(word, MAXWORD) != EOF) if (isalpha(word[0])) if ((n = binsearch(word, keytab, NKEYS)) >= 0) keytab[n].count++; for (n = 0; n < NKEYS; n++) if (keytab[n].count > 0) printf("%4d %s\n", keytab[n].count, keytab[n].word); return 0; }
- tom_mellior 8y agoThanks. I still find these a bit more palatable than your loop-in-an-else-in-a-loop (there are no elses and no loops inside ifs here), but yeah, I can imagine that K&R might have written it the same way as you did.
- foo101 8y agoThe second example does lack braces in two nested 'if' statements. Did you ignore that? Granted it is not three levels deep and granted there is a brace in the 'else' part but that's no reason to ignore the fact that K&R does in fact omit the braces for nested 'if-else' statements. Demanding now that the nesting has to be less than three levels deep to omit braces is just moving the goalposts.
- tom_mellior 8y ago> nested 'if-else' statements Yeah, "else if" is a special case where I wouldn't put braces even if I prefer them everywhere else. > Demanding now that the nesting has to be less than three levels deep I specifically asked for a three levels before, in https://news.ycombinator.com/item?id=19162933 https://news.ycombinator.com/item?id=19162933, not "now".
- foo101 8y agoYour original critique from https://news.ycombinator.com/item?id=19161365 https://news.ycombinator.com/item?id=19161365 said this: > Yes, C doesn't force you to use braces here, but this is really overdoing it. It said nothing about nesting level depth. The question of nesting level appeared later in a child comment of yours which I believe is moving the goalposts. Sorry, I didn't make this clear in my previous comment. Having said that, I do agree with you that braces are helpful. But then, we should all decide the pants policy in our own homes: https://groups.google.com/d/msg/nodejs/MWaivVTirPY/0pnRjKsggkIJ https://groups.google.com/d/msg/nodejs/MWaivVTirPY/0pnRjKsgg...