The meaning of the parameters of the main function

Reprinted from: Click on the link
link 2

Add qq1126137994 WeChat: liu1126137994 to learn more techniques together! ! !

I recently learned server network programming and encountered a problem. The parameters of the main function are specially organized and recorded! ! !

Below is a main function with parameters!

#include <iostream.h>


int main(int argc,char *argv[])
{
       for(inti=0;i<argc;i++)
       {
              cout<<argv[i]<<'\t';              //
       }
       return0;
}
/*

int argc, char **argv 用于运行时,把命令行参数传入主程序。

argc -- 命令行参数总个数,包括 可执行程序名。

argv[i] -- 第 i 个参数。

argv[0] -- 可执行程序名。

*/

About the network collection of the main function with parameters:
int main(int argc,char **argv)The meaning of **argv here
is used to pass parameters. If the file name you compiled into is a.exe, then you can use a.exe12 34 at the command prompt to Pass parameters to the program, then two parameters will be passed to the string array argv, note that three parameters are actually passed at this time, the first parameter is the file name, the second parameter is 12, and the third is 34 , and argc is the number of parameters.

int argc, char **argv 用于运行时,把命令行参数传入主程序。

argc -- 命令行参数 总个数,包括 可执行程序名。

argv[i] --  第 i 个参数。

argv[0] -- 可执行程序名。



// 例如运行:

abc.exe

argc 等于 1, argv[0] 是 "abc.exe"



// 例如运行:

rec.exe  4  5.2

argc 等于 3, argv[0] 是 "rec.exe", argv[1] 是 "4", argv[2] 是 "5.2".

主函数里若有:

int x;

float y;

char s[80];

strcpy(s,argv[0]);  // 程序名存入 了 s

sscanf(argv[1],"%d",&x);  // x 得到数值4

sscanf(argv[2],"%f",&y);  // y 得到数值 5.2

Collected several common forms in C and C++:

(1) main() 
(2) int main() 
(3) int main(void) 
(4) int main(int, char**) 
(5) int main(int, char*[]) 
(6) int main(int argc, char **argv) 
(7) int main(int argc, char *argv[]) 
(8) int main( int argc, char *argv[], char*envp[]) 
(9) void main(void)

(1) is shorthand for (3). Not recommended.
(2) is a shorthand for (3). is the correct form in C++.
(3) is the correct form in both C and C++. Recommended Use. (There is also the main(void) form of the default int).
(4) and (5) are written without parameters. Warn when compiler level is high. Not recommended.
(6) is another way of writing (7). Both are fine, depending on personal preference.
(7) is the correct form with parameters. Recommended Use.
(8) is a rarely used writing method and is limited by the system.
(9) is generally not considered to be the correct way of writing. But it is used in embedded systems (including void main() form)

Since the main function cannot be called by other functions, it is impossible to get the actual value inside the program. So, where do you assign the actual parameter values ​​to the formal parameters of the main function? Actually, the parameter values ​​of the main function are obtained from the operating system command line. When we want to run an executable file, enter the file name at the DOS prompt, and then enter the actual parameters to transfer these actual parameters to the formal parameters of main.

The general form of the command line at the DOS prompt is:

 C:\>可执行文件名  参数  参数……;  

However, it should be noted that the two formal parameters of main and the parameters on the command line do not correspond one-to-one in position. Because, there are only two formal parameters of main, and the number of parameters in the command line is not limited in principle. The argc parameter indicates the number of parameters in the command line (note: the file name itself is also a parameter), and the value of argc is automatically assigned by the system according to the actual number of parameters when the command line is entered.

For example, there is a command line as:

  C:\>E24  BASIC  foxpro  FORTRAN

The argv parameter is an array of string pointers, and each element value is the first address of each string in the command line (parameters are processed as strings). The length of the pointer array is the number of parameters. The initial value of the array elements is automatically assigned by the system. It is represented as shown in the figure:
write picture description here

Another example is to run the following program:
This example is to display the parameters entered on the command line. If the executable file in the above example is named lyy.exe, it is stored in the disk of drive A. So enter the command

C:>a:lyy BASIC foxpro FORTRAN

main(int argc,char *argv){

  while(argc-->1)

      printf("%s\n",*++argv);
return 0
}

Then the running result is:

BASIC

foxpro

FORTRAN

There are 4 parameters in this line. When main is executed, the initial value of argc is 4. The 4 elements of argv are divided into the first addresses of 4 strings. Execute the while statement, decrement the value of argv by 1 every time it loops, stop the loop when argv is equal to 1, and loop three times in total, so a total of three parameters can be output. In the printf function, since the print item *++argv is incremented by 1 and then printed, the string BASIC pointed to by argv[1] is printed for the first time. The second and third loops print the last two strings respectively. The parameter lyy is the file name and does not have to be output.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324851859&siteId=291194637