Main function argc, argv main command line parameters

Source: Internet
Author: User
ARG in ARGc and ARGv indicates "parameters" (ARGuments, argument counter, and argument vector)
There are at least two parameters to the main function: ARGc and ARGv;
The first is a provided parameter to the program, and the second is a pointer to the string array.

Basic functions:
Argc and argv are useful for compiling programs using command lines.
Description of the variable (int argc, char * argv []) in the main function
Some compilers allow declaring the return type of main () as void, which is no longer a valid C ++;
Main (int argc, char * argv [], char ** env) is the standard format in UNIX and Linux.

>>> Argc: an integer used to count the number of command line parameters sent to the main function when you run the program.
>>> * Argv []: string array, used to store a pointer array pointing to your string parameters. Each element points to a parameter.
# Argv [0] points to the full path name of the program running
# Argv [1] points to the first string after the program name is executed in the doscommand line
# Argv [2] points to the second string after the execution program name
...
# Argv [argc] is NULL.

>>> ** Env: string array. Each element of env [] contains characters in the form of ENVVAR = value.
String. ENVVAR is the environment variable and value is the corresponding value of ENVVAR.

Argc, argv, and env are assigned values before the main () function. The executable file generated by the compiler. main () is not a real entry point, but a standard function, this function name is related to the specific operating system.


Example:

# Include <stdio. h> // it must contain <stdio. h> Header file int main (int argc, char * argv []) // integer type main function (number of integer statistical parameters, character type * array pointer to character []) {printf ("% d \ n", argc); // format the output while (argc) // when (number of statistical parameters) printf ("% s \ n ", argv [-- argc]); // format the output return0; // return 0; exit normally}

Suppose you compile it as test.exe
Under the command line
> Test hello

The output result is
2
Hello
Test

Main (int argc, char * argv []), where argc refers to the number of variables. In this example, it refers to the two variables test and hello (note, the command test is also included, argc is 2, argv is an array of char *, which stores pointers to parameter variables. Here argv [0] points to test, argv [1] points to hello


Another example:

# Include <stdio. h> int main (int argc, char * argv []) {if (argc = 1 | argc> 2) printf ("Enter the file name you want to edit, for example: fillname "); if (argc = 2) printf (" Edit % s \ n ", argv [1]); return0 ;}

Compile the program: gcc-o edit. c
Run:> edit
Result: Enter the file name to edit, for example, fillname.
Run:> edit f1.txt
Result: f1.txt is edited.
When editing is executed, argc is 1, argv [0] points to edit
When the edit f1.txtcommand is executed, the value of ARGC is 2, and the value of argv1_0 is directed to editand the value of argv1_1is directed to f1.txt.

Main function argc, argv main command line parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.