Parameters of the main function

xiaoxiao2021-03-19  194

The main function described earlier in the parameter of the main function is without parameters. Therefore, the brackets after Main are empty. In fact, the main function can take parameters, this parameter can be considered as the formal parameter of the main function. The C language specifies that the parameters of the main function can only have two, and these two parameters are written as Argc and Argv. Therefore, the function of the main function can be written as: Main (Argc, Argv) C language also specifies that argc (first ginseng) must be an integer variable, and Argv (second ginseng) must be a pointer to string. Array. After adding the design, the function of the main function should be written as: main (argc, argv) int Argv; char * argv []; or is written: main (int Argc, char * argv []) Because the main function cannot be Other functions are called, so it is impossible to obtain actual values ​​inside the program. So, where do the real gate value give the main parameter? In fact, the parameter value of the main function is obtained from the operating system command line. When we want to run an executable file, type the file name at the DOS prompt, and enter the actual parameters to send these real parameters to the metall paramesis. The general form of the command line of the DOS prompt is: c: /> executable file name parameter parameter parameters ...; but should pay special attention to the parameters in the two ginseng parameters in the command line, the parameters in the position are not one or one of. Because, the metabology of Main is only two, and the number of parameters in the command line is not limited. The ARGC parameter represents the number of parameters in the command line (Note: The file name itself is also a parameter), and the value of the argc is automatically given by the system by the system by the system when entering the command line. For example, there is a command behavior: C: /> E6 24 Basic DBase Fortran Since the file name E6 24 itself is also a parameter, there are four parameters, so the value of the ARGC is 4. The argv parameter is the string pointer array, and the elements values ​​are the first address of each string in the command line (parameters by string processing). The length of the pointer array is the number of parameters. The initial value of array element is automatically given by the system. It is shown in Figure 6.8: main (int Argc, char * argv) {while (argc -> 1) Printf ("% s / n", * argv);} This example is the input to the command line. Parameters If the executable file of the previous example is E24.exe, store it in the A drive. So the input command behavior: C: /> A: E24 Basic DBase Fortran runs the result to: BasicDBasefortRan This line has 4 parameters. When performing MAIN, the initial value of Argc is 4. The 4 elements of Argv are divided into four strings of the first address. Execute the While statement, each cycle ARGV value is reduced by 1, stop the loop when Argv is equal to 1, cyclic cycle, therefore, three parameters can be output. In the Printf function, since the print item * Argv is plus 1 printed, the first print is the string Basic referred to as Argv [1]. Second, the three cycles are printed separately two strings. The parameter E24 is the file name and does not have to be output. There are two parameters in the command line of the following example, and the second parameter 20 is the input N value. In the program * Argv value is a string "20", then use the function "ATOI" to switch it into a loop control variable in the While statement, and output 20 orientations.

转载请注明原文地址:https://www.9cbs.com/read-130148.html

New Post(0)