The parameters of Main in the C program

xiaoxiao2021-03-06  46

The parameters of Main in the C program

The program of the command line interface usually needs to enter the command line parameter help program execution. Assume that there is a executable program name Test. Then the command line running the program is as follows:

Test

The command line parameters are additional items in the same line:

Test -c test

Where -C and TEST are command line arguments. The C program can read these additional parameters and use it for yourself, such as a condition that runs as a program (often seeing the debug parameter -D is such). The C program reads these additional parameters by using the parameters of Main (). The following REPEAT.C gives an example of reading the main parameter:

Repeat.c:

#include

#include

Int main (int Argc, char * argv [])

{

INT country;

Printf ("THE COMMAND LINE HAS% D Arguments: / N", ARGC - 1);

For (count = 1; count

{

Printf ("% d:% s / n", count, argv [count]);

}

Printf ("/ n");

// system ("pause");

Return 0;

}

Here first explains the meaning of the two parameters in this function, and the argc records the number of input parameters in the command line. Argv is a string array of argc elements, each The element saves the parameters entered in a command line.

Compiling this file for the executable REPEAT:

GCC REPEAT.C -O REPEAT

Execute the REPEAT program in the following manner

./repeat i "love you" 3

The output is as follows:

The Command Line Has 3 Arguments:

1: i

2: Love you

3: 3

In this example, the value of Argc is 4, and four parameters are entered in a total of the command line "./repeat" "^", "3". In the DOS and UNIX environments, "" symbols are used in the command line parameter indicate it is a string, which is considered a parameter.

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

New Post(0)