Merrill said:
Today is when I sing in the choir, and I was thinking about rehearsal
tonight when I realized that in order to be in the C choir, I had to buy K&R
to be reading the same "music."
I am familiar with
http://www.eskimo.com/~scs/C-faq/top.html , but I found
no resolution to my first question, which crops up on page 6. I have always
called main with an integer that says how many pointers are being passed to
it. Is this just a matter of style?
Would int main(int a, int b, int c){ return (51);} be, while close to
trivial, ANSI compliant? MPJ
Not in a "hosted environment," which covers the vast
majority of general-purpose C implementations. Such an
implementation launches a program by taking care of some
necessary housekeeping (for example, initializing all
your `static' variables, setting up stdin/out/err, and
so forth) and then calling the main() function that you
supply. The implementation uses Implementation Magic(tm)
to accomplish all this, but isn't Magical enough to read
your mind about what sort of arguments your main() wants,
so it always provides an `int' and a `char**'. If your
main() expects something else, unpredictable confusion
will arise.
Actually, there is one further bit of Magic: You can
write your main() to expect no arguments at all, and the
implementation is required to call it anyhow. Thus, there
are two valid "signatures" for main():
int main(int, char**);
int main(void);
(There are, of course, other ways to express these two
possibilities.)
Now, a particular C implementation may also support
other main() signatures in addition to the two required
by the language -- a common extension is
int main(int, char**, char**);
where the third (non-standard) argument points to an
array of "environment variables." The Standard does not
forbid such alternate forms for main(), but also does not
require them. You can use other forms if your platform
supports them, but all bets are off when you move your
code to a different platform.
All this pertains to "hosted environments." The C
Standard also considers "free-standing environments," the
sort of C implementation that might operate your microwave
oven or your talking toilet. (I'm not kidding; use Google.)
These environments make their own provisions for invoking
and terminating programs, and main() may have a completely
different form -- it may not even have any special meaning
at all. Indeed, there may be no such concept as "program
start" or "program finish;" the program may simply run as
long as the electricity keeps flowing.
Finally, a word about all that Implementation Magic(tm).
I've said it "calls main()," but in fact the actual means by
which main() begins to execute need not be a bona-fide C
language function call. The implementation must make it
appear "as if" main() has been called from a higher-order
function, but the means by which this happens could be
mysterious in the extreme. You really needn't worry about
this because once your main() receives control it seems to
have been called in ordinary fashion; I'm only mentioning
this to keep my fellow pedants off my case.
To return to your music analogy, when you're "hosted"
by a choir with an organ and/or piano and/or orchestra,
tuning to A = 440Hz is all but mandatory; you use the
standard forms for main(). Some groups with an interest in
early music may deliberately use older tunings like A=435Hz;
some platforms support alternate main() forms. And when
you're singing in the shower, "Nessun dorma" sounds a lot
better if you slide that A down to 370Hz or so; there are
no standards for main() in a free-standing shower stall.