Meaning of parameters in int main (INT argc, char * argv [])

Source: Internet
Author: User
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:

  /*program name EXAMPLE.EXE*/ 
#include
#include
main(int argc, char *argv[], char *env[])
{
int i;
printf("These are 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 this system are:\n\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:

#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 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.

Click here for the original article

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.