"Bill Cunningham" <no****@nspam.net> wrote in message
news:40********@corp.newsgroups.com...
Re: Main [was File IO]
Since your question is about an entirely different topic
from the original one of this thread, imo you should have
started a new thread. Whatever... See below.
int main(int argc, char *argv[]) {
I'm looking at Giannis's code here and I see the one thing in C I dread
the most. I avoid even looking at it if I can. But one must learn main (int
argc, char *argv[]). Those main parameters look like garbage to me, when
would one use them. Argc must mean an argument to main, the array pointer
to a char pointer leaves me clueless. I understand main(void). What's the
purpose of the other parameters?
They're for accessing a program's (typically 'command line')
parameters.
================================================== ============
ISO/IEC 9899:1999 (E)
[....]
5.1.2.2.1 Program startup
1 The function called at program startup is named main. The
implementation declares no prototype for this function. It
shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the
function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;(9) or in some other implementation-defined
manner.
2 If they are declared, the parameters to the main function
shall obey the following constraints:
-- The value of argc shall be nonnegative.
-- argv[argc] shall be a null pointer.
-- If the value of argc is greater than zero, the array
members argv[0] through argv[argc-1] inclusive shall
contain pointers to strings, which are given implemen-
tation-defined values by the host environment prior to
program startup. The intent is to supply to the program
information determined prior to program startup from
elsewhere in the hosted environment. If the host envi-
ronment is not capable of supplying strings with letters
in both uppercase and lowercase, the implementation shall
ensure that the strings are received in lowercase.
-- If the value of argc is greater than zero, the string
pointed to by argv[0] represents the program name;
argv[0][0] shall be the null character if the program name
is not available from the host environment. If the value
of argc is greater than one, the strings pointed to by
argv[1] through argv[argc-1] represent the program
parameters.
-- The parameters argc and argv and the strings pointed to
by the argv array shall be modifiable by the program,
and retain their last-stored values between program startup
and program termination.
[....]
(9) Thus, int can be replaced by a typedef name defined as int,
or the type of argv can be written as char ** argv, and so on.
[....]
================================================== ============
Example use:
#include <stdio.h>
int main(int argc, char **argv)
{
const char * const messages[] =
{
"[not available]",
"[none given]",
""
};
const char * const *pmsg[] = {messages, argv};
int i = 0;
printf("argc == %d\n\n", argc);
printf("Program name:\n%s\n\n",
argc > 0 ? *pmsg[**argv != 0]
: messages[argc > 0]);
printf("Parameters:\n");
for(i = 1; i < argc; ++i)
printf("[%d] == %s\n", i, argv[i]);
printf("%s\n", messages[(argc > 1) + (argc > 0)]);
return 0;
}
-Mike