command line parameters in C argc,argv[]

Source: Internet
Author: User
Tags first string

Main (int Argc,char *argv[])

argv Pointer to pointer

ARGC is an integer

Char **argv Or:char *argv[] Or:char argv[][]

Main () is a fixed notation within parentheses.

An example is given below to understand the use of these two parameters:

Assuming that the program name is prog,

When only prog is entered, the parameters that are transmitted by the operating system are:

Argc=1, which indicates that there is only one program name.

ARGC has only one element, Argv[0] points to the program path and name of the input:./prog

When input prog para_1, there is a parameter, then the parameters transmitted by the operating system are:

Argc=2, which indicates that there is a parameter in addition to the program name.

Argv[0] points to the program path and name of the input.

ARGV[1] points to the parameter para_1 string.

When the input prog para_1 para_2 has 2 parameters, the parameters that are transmitted by the operating system are:

Argc=3, which indicates that there are 2 parameters in addition to the name of the program.

Argv[0] points to the program path and name of the input.

ARGV[1] points to the parameter para_1 string.

ARGV[2] points to the parameter para_2 string.

void Main (int argc, char *argv[])

Char *argv[]: argv is an array of pointers, the number of his elements is argc, storing pointers to each parameter,

His first element is argv[0] The executable file name (including path eg: "F:/vc/ex1/debug/ex1.exe") generated for the compilation,

Starting with two elements (Argv[1]),

is each parameter int argc represents the size of the argv, which is the actual number of parameters +1,

Where +1 is because Argv[0] is the compiled executable file name

Main () Primary function

Every C program must have a main () function, which can be placed in a program according to its own hobby
A place. Some programmers put it at the top, while others put it on the last side, regardless of whether they put
In which area, the following points are appropriate.

1. Main () parameter
During the turbo C2.0 boot process, pass the main () function three parameters: ARGC, argv, and Env.
* ARGC: Integer, the number of command-line arguments passed to main ().
* argv: string array.
In the DOS 3.X version, argv[0] is the full path name for the program to run; To DOS 3.0
The following version, Argv[0] is an empty string ("").
ARGV[1] is the first string after executing a program name on a DOS command line;
ARGV[2] is the second string after the execution of a program name;
...
ARGV[ARGC] is null.
*env: Array of An Fu strings. Each element of env[] contains characters in the form of Envvar=value
String. Where Envvar is an environment variable such as path or 87. Value corresponds to Envvar values such as C:/dos, C:
/TURBOC (for path) or Yes (for 87).
The Turbo C2.0 always passes these three parameters to the main () function when it is started, and can be used in the user program
Descriptions (or not), they become main () subroutines if some (or all) parameters are described
The local variable.
Please note: Once you want to illustrate these parameters, you must press ARGC, ARGV, env order, as follows
Examples of:
Main ()
Main (int argc)
Main (int argc, char *argv[])
Main (int argc, char *argv[], char *env[])
The second case is legal, but not uncommon, since in the program there is very little use of argc, not
Use of argv[].
The following example program, EXAMPLE.EXE, shows how to use three parameters in the main () function:
/*program name EXAMPLE. exe*/
#i nclude
#i nclude
Main (int argc, char *argv[], char *env[])
{
int i;
printf ("These is the%d command-line arguments passed to
main:/n/n ", argc);
for (i=0; i<=argc; i++)
printf ("argv[%d]:%s/n", I, argv[i]);
printf ("/nthe environment string (s) on the This system are:/n/n");
for (i=0; env[i]!=null; i++)
printf ("env[%d]:%s/n", I, env[i]);
}
If at the DOS prompt, run EXAMPLE.EXE as follows:
C:/example first_argument "argument with blanks" 3 4 "last but
One "stop!
Note: You can enclose an argument with a space in double quotation marks, as in this example: "Argument
With blanks "and" last but one ").
The result is this:
The value of ARGC is 7
These is 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!

_______________________________________________________

command-line interface, which usually requires the input of a command line parameter helper execution. Assume that there is an executable program named Test. Then the command line to run the program is as follows:
Test
With command line arguments is an additional item in the same row:
Test–c TEST
Where –c and test are command-line arguments. C programs can read these additional parameters, and for their own use, for example, as a program running conditions (often see debugging parameters –D is such a). The C program reads these additional parameters by using the parameters of Main (), and the following repeat.c gives an example of reading the main parameter:
REPEAT.C:
#include
#include
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 explain the meaning of the two parameters in the function of main (int argc, char*argv[]), ARGC records the number of input parameters in the command line, argv is an array of strings with argc elements, each element holds a parameter entered on the command line.
Compile this file as an executable file repeat:
GCC Repeat.c-o Repeat
Execute 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
3:3
In this example, the value of ARGC is 4, and the command line has entered a total of four parameters "./repeat", "I", "Love You", "3". In DOS and UNIX environments, the "" symbol in a command-line argument indicates that it is a string and is treated as a parameter.

command line parameters in C argc,argv[]

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.