Eric Sosman said:
The fundamental problem, Québec, is that you do not
understand the tool you are trying to use. You don't know
any C, yet you're trying to program in C. Lacking knowledge,
you throw together miscellaneous bits and pieces that look
just a little bit like C, then you stir them randomly until
the compiler stops complaining, and then you wonder why they
behave in peculiar ways.
Perhaps you think this is a good way to proceed -- after
all, it's the way natural selection works, and the organisms
evolution has produced are marvellous. Unfortunately, natural
selection requires a lot of time. A *lot* of time, as in
billions of years. I don't know about your schedule, but my
own doesn't permit me to wait that long for a working program.
To expand on that point a bit ...
Different programming languages vary in the amount of checking that is
done, or that can be done, before you can actually run the program.
Suppose a programmer writes, say, several hundred lines of code before
first trying to compile it. The compiler is likely to report a number
of errors: typos, syntax errors, missing declarations, etc. The
programmer then fixes the reported errors and tries compiling again,
iterating until the compilation is successful.
In some languages, once you've gotten the code to compile cleanly,
there's a fairly decent chance that it will actually work. (<OT>In my
experience, Ada is such a language; Cobol may be another, but I've
never used it.</OT>) That's not to say that logical errors are
impossible in such languages, but many kinds of errors are likely to
be caught by the compiler. (I'm absolutely not trying to start a
language war, just making an observation.)
C is not such a language. There are a number of seemingly minor
errors you can make in C that will break the program, or at least
change its meaning. Consider:
#include <stdio.h>
int main(void)
{
int i;
for (i = 0; i < 10; i ++)
printf("i = %d\n", i);
printf("========================================\n");
for (i = 0; i < 10; i ++);
printf("i = %d\n", i);
printf("========================================\n");
for (i = 0; i < 10; i ++)
printf("i = %d", i);
printf("\n");
return 0;
}
It's easy for a novice programmer to assume that each of the three
blocks of code starting with "for" is going to do the same thing, but
the second and third behave very differently. (Do you see why?) Both
gcc with all warnings enabled and Splint accept the above program
without complaint. A code formatter is likely to show the problem
(that's a hint).
The C philosophy, to the extent that there is such a thing, is to
assume that the programmer knows what he's doing. That makes it easy
for an experienced programmer to write correct code, but it also makes
it easy for any programer, experienced or not, to make mistakes.