G
Geoff
Why? I've read several other followups in this thread, and I
haven't seen you provide any supporting arguments for this.
at_beginning is treated as a boolean. It would make sense to declare it
as bool or _Bool if the compiler supports it. Why is an explicit
comparison against 1 better than a simple test of the variable?
"if (at_beginning)" expresses the intent more clearly than
"if (at_beginning == 1)". And since "==" yields a logically boolean
result, why stop at one "=="; why not write
"if ((at_beginning == 1) == 1)"?
In this particular case, at_beginning can only have the value 0 or
1, but of course any non-zero value is treated as true. What if,
in a later version of the program, at_beginning is assigned the
result of one of the is*() functions?
I presume there's some reason behind your reaction, but I honestly
can't think of what it might be. Yes, the OP made an "=" vs. "=="
error. Surely the solution is to avoid making such errors --
which, I think, are no more likely with the suggested change than
without it.
I wouldn't call it appalling but I would call it idiomatic and
somewhat dangerous if it were to appear in an introductory C textbook.
Far from being easily read, it sets the beginner back on his chair
with a "Woah, what's going on here?"
The insistence that this idiom is preferable calls into question why
no one has suggested replacing
line += 1;
with
line++;
Furthermore, as pertains to the OP's text, this program has formed the
basis of another program regarding checksum, posted here.
<c27675a6-22a4-4432-9481-6a3618dbe362@glegroupsg2000goo.googlegroups.com>
Had the OP implemented the change Ben suggested, the program would not
have been modifiable from this:
if(ch == '\n')
at_beginning = 1;
to this:
if(ch == '\n'){
at_beginning = 1;
printf("Checksum: %d\n", checksum);
checksum = -1;
}
So from a maintenance standpoint, Ben's idiom is "less maintainable"
in this context.