C language - main handles command line options/parameters of the main function

1. Concept Supplement

1.1 main function form

//写法一:
int main(int argc, char* argv[])//这里使用char* argv[]
{
    
    
    return 0;
}
//写法二:
int main(int argc, char** argv)//这里使用char ** argv
{
    
    
    return 0;
}

1.1 Parameter description of the main function

The type of the first parameter is int ;
the second parameter is an array of strings .
Usually, the first parameter is named argc, the second parameter is argv, and the parameter names are defined by themselves.

int argc : the number of strings in the array argv, the number of strings argc=1+the number of strings entered by the user, the operating system is responsible for calculating the number, the programmer does not care, just need to use it correctly, for example, the user enters 2 characters string, argc=1+2=3;
char *argv[ ] : array of strings, that is, multiple strings, the form is as follows:

注意: 最终操作系统存储的字符串数组为, 
argv[0]=可执行文件名称,例如test.exe 
argv[1]= 字符串1 
argv[2]=字符串2 
argv[3]=字符串3 

If user input is to be used, the first string should be argv[1], not argv[0]

Note:
The second parameter is a series of strings entered by the user, and the strings are separated by spaces, in the form:
string 1 string 2 string 3
Note: the operating system will automatically add a string (program name) to the character string array, therefore, the final string number array length is N+1.
When code encoding uses string content, you need to pay attention to the index number of the string :
if you want to use the first string entered by the user, it should be argv[1] instead of argv[0]

Guess you like

Origin blog.csdn.net/DADWAWFMA/article/details/127111494