The meaning of the main function main (int Argc, char * argv [])

xiaoxiao2021-03-05  20

The mean of the main function main (int Argc, char * argv []) has seen the source code of the software under multiple Linux, and found that many projects have main (int Argc, char * argv []), found online instruction of.

When I just came into contact with these two variables, I didn't know what they used to do. I think many people are like me, just seeing these two variables are also rare. In fact: int Main (int Argc, char * argv []) is a standard writing method in UNIX and Linux, and int main () is just the usage of UNIX and Linux acquisition .. What is the use of argc, argv []? Let's take an example edit.c will understand their usage: #include #include int main (int Argc, char * argv []) {if (argc == 1 | Argc> 2) {Printf ("Please enter the file name you want to edit, such as: ./ Edit Fillen");} if (argc == 2) {Printf ("Edit% SN", Argv [1]);} exit (0)} Compile the program: gcc -o edit edit.c Run: ./ Edit Result: Please enter the file name you want to edit such as: ./ Edit Fille Run: ./ Edit Edit.txt Result: Edit Edit.txt Seeing this, Argc, Argv [] I understand, the argc is the number of external command parameters, argv [] stores the contents of each parameter, as in the above example: Perform ./edit, argc is 1, argv [0] For ./EDIT ./edit edit.txt, the value of Argc is 2, argv [0] is ./edit ,arv[1] is Edit.txt.

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

New Post(0)