Program entry function
int main( void ); int main( int argc, const char *argv[] ); int main( int argc, const char *argv[], char *envp[] );
The arguments depend on which form of main() that you use.
The main() function is supplied by the user and is where program execution begins. The command line to the program is broken into a sequence of tokens separated by blanks, and are passed to main() as an array of pointers to character strings in argv. The number of arguments found is passed in the parameter argc.
The argv[0] argument is a pointer to a character string containing the program name. The last element of the array pointed to by argv is NULL (argv[argc] is NULL). Arguments containing blanks can be passed to main() by enclosing them in quote characters (which are removed from that element in the argv vector). See your shell's documentation for details.
The envp argument points to an array of pointers to character strings that are the environment strings for the current process. This value is identical to the environ variable, which is defined in the <stdlib.h> header file.
A value back to the calling program (usually the operating system).
#include <stdio.h> #include <stdlib.h> int main( int argc, char **argv ) { int i; for( i = 0; i < argc; ++i ) { printf( "argv[%d] = %s\n", i, argv[i] ); } return EXIT_SUCCESS; }
produces the output:
argv[0] = ./mypgm argv[1] = hhhhh argv[2] = another arg
when the program mypgm is run from the shell:
$ ./mypgm hhhhh "another arg"
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
abort(), atexit(), _argc, _argv, _auxv, close(), execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), _exit(), exit(), getenv(), putenv(), sigaction(), signal(), spawn(), spawnl(), spawnle(), spawnlp(), spawnlpe(), spawnp(), spawnv(), spawnve(), spawnvp(), spawnvpe(), system(), wait(), waitpid()