by tom_mellior · 7y ago · 0 comments
for (i = 0; i < sizeof love / sizeof *love; i++)
if (love[i] < 4)
printf("%s", say[love[i]]);
else
for (j = 0; j < love[i] / 4; j++)
putchar(show[love[i] % 4]);
Yes, C doesn't force you to use braces here, but this is really overdoing it.And I say this as someone who does like Python's indentation-based syntax. Doing the same in C is not idiomatic.
Yeah, well... I am lonely. :(
It's the style in K&R? Ok, so the literal inventor of the language was certain he wouldn't make this kind of mistake, that still doesn't mean I trust myself not to.
#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.
Good thing we can indicate our love with persnickety comments and have no need for such a program.
It's exactly as you might see in K&R, and short enough that there's never going to be confusion from their lack.