int main(int argc,char * argv[])
Argv is a pointer.
Argc is an integer
char **argv or: char *argv[] or: char argv[][]
The main () brackets are fixed.
The following is an example to understand the usage of these two parameters:
Assume that the program name is prog,
If you only enter prog, the parameters sent from the operating system are:
Argc = 1, indicating that there is only one program name.
Argc has only one element. argv [0] points to the input program path and name:./prog
When you enter prog para_1, there is a parameter, the parameter sent from the operating system is:
Argc = 2, indicating that there is a parameter besides the program name.
Argv [0] points to the input program path and name.
Argv [1] points to the para_1 string.
When the input prog para_1 para_2 has two parameters, the parameters sent from the operating system are:
Argc = 3, indicating that there are two parameters besides the program name.
Argv [0] points to the input program path and name.
Argv [1] points to the para_1 string.
Argv [2] points to the para_2 string.
Void main (INT argc, char * argv [])
Char * argv []: argv is a pointer array. Its number of elements is argc, which stores pointers to each parameter,
The first element of argv [0] is the executable file name generated by compilation (including path eg: "F: \ Vc \ ex1 \ debug \ ex1.exe "), starting from two elements (argv [1]), each parameter int argc indicates the size of argv, which is the actual number of parameters + 1, here, + 1 is because argv [0] is the executable file name after compilation.
Main () Main Function
Every C program must have a main () function, which can be placed somewhere in the program based on your hobbies. Some programmers put it at the beginning, while others put it at the end, regardless
Which of the following statements is applicable.
1. Main () parameter
During Turbo c2.0 startup, three parameters of the main () function are passed: argc, argv, and Env.
* Argc: an integer that represents the number of command line parameters passed to main.
* Argv: String Array.
In dos 3.x, argv [0] is the full path name of the program running. For DoS 3.0
In the following versions, argv [0] is an empty string ("").
Argv [1] is the first string after the program name is executed in the doscommand line;
Argv [2] is the second string after the execution program name;
...
Argv [argc] is null.
* Env: an array of security strings. Each element of env [] contains a character string in the form of envvar = value. Envvar is an environment variable, such as path or 87. Value is the corresponding value of envvar, such as c: \ dos, c: \ turboc (for Path) or yes (for 87 ).
When Turbo c2.0 is started, these three parameters are always passed to the main () function, which can be described (or not described) in the user program. If some (or all) parameters are specified, they become local variables of the main () subroutine.
Note: Once you want to describe these parameters, they must be in the order of argc, argv, and env, as shown in the following example:
Main ()
Main (INT argc)
Main (INT argc, char * argv [])
Main (INT argc, char * argv [], char * env [])
The second case is legal, but not common, because argc is rarely used in programs, rather than argv.
The following provides the example program example. EXE to demonstrate how to use three parameters in the main () function:
[CPP]View plaincopy
- /* Program name example. EXE */
- # Include <stdio. h>
- Main (INT argc, char * argv [], char * env [])
- {
- Int I;
- Printf ("these are the % d command-line arguments passed to main: \ n", argc );
- For (I = 0; I <= argc; I ++)
- Printf ("argv [% d]: % s \ n", I, argv [I]);
- Printf ("\ nthe environment string (s) on this system are: \ n ");
- For (I = 0; ENV [I]! = NULL; I ++)
- Printf ("env [% d]: % s \ n", I, ENV [I]);
- }
Run example. EXE as follows at the DOS prompt:
C: \ example first_argument "argument with blanks" 3 4 "last but one" Stop!
Note: Double quotation marks can be used to enclose parameters with spaces. For example, "argument with blanks" and "last but one ").
The result is as follows:
The value of argc is 7
These are the 7 command-linearguments passed to main:
Argv [0]: C: \ Turbo \ example. exe
Argv [1]: first_argument
Argv [2]: argument with blanks
Argv [3]: 3
Argv [4]: 4
Argv [5]: last but one
Argv [6]: Stop!
_______________________________________________________
The command line interface usually needs to input command line parameters to help the program to execute. Suppose there is an executable program named test. The command line for running the program is as follows:
Test
The parameters with the command line are additional items in the same line:
Test-C Test
-C and test are command line parameters. C Programs can read these additional parameters and use them for their own purposes, for example, as a condition for running the program (the debugging parameter-D is often seen as such ). C program reads these additional parameters by using the main () parameters. The following repeat. C provides an example of reading the main parameter:
Repeat. C:
[CPP]View plaincopy
- # Include <stdio. h>
- Int main (INT argc, char * argv [])
- {
- Int count;
- Printf ("the command line has % d arguments: \ n", argc-1 );
- For (COUNT = 1; count <argc; count ++)
- {
- Printf ("% d: % s \ n", Count, argv [count]);
- }
- Printf ("\ n ");
- // System ("pause ");
- Return 0;
- }
Here we will first explain the significance of the two parameters in the main (INT argc, char * argv []) function. argc records the number of input parameters in the command line, argv is a string array with argc elements. Each element stores the input parameters in a command line.
Compile this file as an executable file repeat:
GCC repeat. C-o repeat
Run the repeat program as follows:
./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. The command line inputs four parameters:./repeat, I, love you, and 3 ". In dos and UNIX environments, the command line parameters are represented as a string with the "" symbol, which is considered as a parameter.
Description of int main (INT argc, char * argv [])