The main () function with parameters in C language, main
Sample Code:
1 # include <stdio. h> 2 3 int main (int argc, char * argv []) 4 {5 int I; 6 printf ("Total % d arguments \ n", argc ); 7 for (I = 0; I <argc; I ++) 8 {9 printf ("Argument % d = % s \ n", I + 1, argv [I]); 10} 11 12 system ("pause"); 13 return 0; 14}
Parameter description:
Argc: number of parameters. If a parameter is not passed to the main () function, the default value is 1. That is, at least one parameter is the name of the executable file (including the directory ).
Argv: A pointer array pointing to the first address of each string parameter, where argv [0] stores the first address of the executable file name.
Save the preceding code as main.c and compile it. Generate the executable file main.exe (under the root directory of drive d). Execute the following command on the cmd command line:
D: \> main.exe hello world
Total 3 arguments
Argument 1 = main.exe
Argument 2 = hello
Argument 3 = world
Press any key to continue...
Tip: In win7, click Start. In the search box, Enter cmd and press enter to open the cmd window. Enter D at the prompt to switch to the root directory of drive D.