Wednesday, June 22, 2011

Main Function in C and C++

  • In many programming languages, the main function is where a program starts execution.
  • typically has access to the command arguments given to the program when it was executed.
  • The main function is generally the first programmer-written function run when a program starts, and is invoked directly from the system-specific initialization contained in crt0 or equivalent.
  • In C and C++, the function prototype of the main function looks like one of the following:
    int main(void)
     
    int main(int argc, char *argv[])
    
     
  • Unix (though not POSIX.1) and Microsoft Windows have a third argument giving the program's environment, otherwise accessible through getenv in stdlib.h:
     
    int main(int argc, char **argv, char **envp)
     
      
    • Mac OS X and Darwin have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:

    int main(int argc, char **argv, char **envp, 
    char **apple)

      The name of the variable argc stands for "argument count"; argc contains the number of arguments passed to the program. The name of the variable argv stands for "argument vector". A vector is a one-dimensional array, and argv is a one-dimensional array of strings. Each string is one of the arguments that was passed to the program. envp specifies the envrionmental variables for the program .
     

No comments:

Post a Comment