1.3.2 Program Startup

A program begins by calling the function main. There is no prototype required for this. It can be defined with no parameters such as:

int main(void) { body… }

Or with the following two parameters:

int main(int argc, char *argv[]) { body… }

Note that they do not have to be called argc or argv, but this is the common naming system.

argc is a nonnegative integer. If argc is greater than zero, then the string pointed to by argv[0] is the name of the program. If argc is greater than one, then the strings pointed to by argv[1] through argv[argc-1] are the parameters passed to the program by the system.

Example:


#include<stdio.h>

int main(int argc, char *argv[])
{
  int loop;

  if(argc>0)
    printf("My program name is %s.\n",argv[0]);

  if(argc>1)
   {
    for(loop=1;loop<argc;loop++)
      printf("Parameter #%i is %s.\n",loop,argv[loop]);
   }
}