Those are for passing arguments to your program, for example from command line, when a program is invoked
$ gcc mysort.c -o mysort
$ mysort 2 8 9 1 4 5
Above, the program mysort
is executed with some command line parameters. Inside main( int argc, char * argv[])
, this would result in
Argument Count, argc = 7
since there are 7 arguments (counting the program), and
Argument Vector, argv[] = { "mysort", "2", "8", "9", "1", "4", "5" };
Following is a complete example.
$ cat mysort.c
#include <stdio.h>
int main( int argc, char * argv [] ) {
printf( "argc = %d\n", argc );
for( int i = 0; i < argc; ++i ) {
printf( "argv[ %d ] = %s\n", i, argv[ i ] );
}
}
$ gcc mysort.c -o mysort
$ ./mysort 2 8 9 1 4 5
argc = 7
argv[ 0 ] = ./mysort
argv[ 1 ] = 2
argv[ 2 ] = 8
argv[ 3 ] = 9
argv[ 4 ] = 1
argv[ 5 ] = 4
argv[ 6 ] = 5
[The char
strings "2", "8" etc. can be converted to number using some character to number conversion function, e.g. atol()
(link)]