#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.
The second example uses braces (as it must), so it's not an example of what I was asking for.
§ 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;
}Demanding now that the nesting has to be less than three levels deep to omit braces is just moving the goalposts.
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, not "now".
> 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/0pnRjKsgg...