Int main (INT argc, char * argv [])

Source: Internet
Author: User
Argc is the total number of parameters in the command line.
Argv [] is an argc parameter, of which 0th is the full name of the program, and subsequent Parameters
Parameters entered by the user following the command line, such:
Int main (INT argc, char * argv [])
{
Int I;
For (I = 0; I <argc; I ++)
Cout <argv [I] <Endl;
Cin> I;
Return 0;
}
During execution
F: \ mydocu ~ 1 \ tempcode \ D1 \ debug \ d1.exe AAAA bbb ccc ddd
The output is as follows:
F: \ mydocu ~ 1 \ tempcode \ D1 \ debug \ d1.exe
Aaaa
Bbb
CCC
Ddd
--------------------------------------------------------------------
Char * argv [] is a character array whose size is int argc. It is mainly used for the argv [] parameter of the command line parameter. Each element in the array represents a parameter;
For example
Test A. c B. C T. C
Then
Argc = 4

Argv [0] = "test"
Argv [1] = "a. c"
Argv [2] = "B. C"
Argv [3] = "T. C"
Bytes --------------------------------------------------------------------------------------------
Argc records the number of parameters input by the user in the command line of the running program.
Arg [] points to at least one character pointer in the array, that is, Arg [0]. It usually points to the executable file name in the program. In some versions of compilers, programs are also included.
File Path.
-------------------------------------------------------------------------
When calling an executable program, parameters must be passed to the program in some cases. For example, you can click notepad.exe on the console,
Press enter to run the Notepad program. If you want to open a notepad file at the same time when you open notepad.exe, you can
The name of the file, for example, notepad.exe example.txt (the file is in the current path ).

How can we obtain these input parameters in the program? This is done by the compiler. the compiler will input the parameter information.
In the parameter list of the main function.

The parameter list of the main function stores the input parameter information. The first parameter argc records the number of input parameters,
The second parameter is a string array. Each unit of the string array is of the char * type and points to a C-style string.
Take notepad.exe example.txt as an example.
Argc is 2, which means there are two valid units in the argv array.
The first unit points to the string "notepad.exe"
The second unit points to the string "example.txt"

The first unit in the argv array points to a string that is always the name of the executable program. The subsequent unit points to a string that is the parameter used for Program Calling in turn.

The assignment process is completed by the compiler. We only need to read the data.
-----------------------------------------------------------------------------

Int main (INT argc, char * argv [], char * envp [])
The main () function generally uses Int or void. I prefer to use the int type to define main. Because at the end of the operation, a value can be returned to the operating system to indicate execution.

Int argc
This parameter indicates the total number of parameters you input in the command line. After your program is compiled, the executable file is test.exe.
D: \ TC2> Test
At this time, the value of argc is 1.
However
D: \ TC2> test.exe myarg1 myarg2
The value of argc is 3. That is, the command name is added with two parameters, three parameters in total

Char * argv []
This is used to obtain the parameters you entered.
D: \ TC2> Test
At this time, the value of argc is 1, and the value of argv [0] is "test"
D: \ TC2> test myarg1 myarg2
At this time, the value of argc is 3, the value of argc [0] is "test", and the value of argc [1] is "myarg1 ", the value of argc [2] is "myarg2 ".
This stuff is generally used to provide very important information for programs, such as data file names.
For example, copy a. c B .txt.
At this time, a.cand B .txt are the so-called "very important information ". If you do not specify these two files, you cannot copy them.
When your program uses the argc and argv parameters, you can simply judge the value of argc to see if the program parameters meet the requirements.

Char * envp []
This stuff is rarely used. It is used to obtain the environment variables of the system.
For example, in DOS, there is a path variable. When you enter a command at the DOS prompt (of course, this command is not an internal command like DIR), DOS will first find the execution file of this command in the current directory. If NO, go to the path defined in path and find it. If no, run the command. If no, bad command or file name is returned.
Enter set at the doscommand prompt to view the environment variables of the system.
Similarly, in Unix or Linux, there are also system environment variables that are used more than DOS. Such as $ path, $ user, and $ home.
Envp stores all environment variables. The format is (under UNIX)
Path =/usr/bin;/local/bin;
Home =/home/shuui
That is:
Environment variable name = Value
The same may be true for DOS.
Environment variables are generally used to provide additional information for programs. For example, you have developed a program to display text content. You want to control the number of characters displayed in a row. You can define an environment variable by yourself (in UNIX)
% Setenv number = 10
% Echo $ number
10
Then you can read the environment variable in the program. Then, the number of characters in a row is determined based on the value. In this way, if you do not modify the environment variable, each time you run this program, the number of characters displayed in a row is different.
The following is an example Program


# Include <stdio. h>
Int main (INT argc, char * argv [], char * envp [])
{
Int I;

Printf ("You have inputed Total % d argments \ n", argc );
For (I = 0; I <argc; I ++)
{
Printf ("Arg % d: % s \ n", I, argv [I]);
}

Printf ("the follow is envp: \ n ");
For (I = 0; * envp [I]! = '\ 0'; I ++)
{
Printf ("% s \ n", envp [I]);
}
Return 0;
}


D: \> argtest this is a test programe of main ()'s argments
You have inputed total 9 argments
Arg0: D: \ TC \ noname. exe
Arg1: This
Arg2: Is
Arg3:
Arg4: Test
Arg5: programe
Arg6:
Arg7: Main ()'s
Arg8: argments
The follow is envp:
TMP = c: \ windows \ Temp
Temp = c: \ windows \ Temp
Prompt = $ p $ G
Winbootdir = C: \ WINDOWS
Path = c: \ windows; C: \ WINDOWS \ command
Comspec = c: \ windows \ command. com
Sbpci = c: \ sbpci
WINDIR = C: \ WINDOWS
Blaster = a220 i7 D1 H7 P330 T6
Using line = noname this is a test programe of main ()'s argments
Bytes -----------------------------------------------------------------------------------------
Command line parameters. Argc is the number of parameters, argv [] is the parameter, argv [0] is the file name, argv [1] is the first parameter...
If your EXE file name is myprog.exe
Myprog 12 22 32
Then argv [0] = "myprog", argv [1] = "12", argv [2] = "22 "...

Exit () is the return code when the program exits. It can be received by other programs to determine whether the program Exits normally. Such as exit (-1.
Bytes ---------------------------------------------------------------------------------------------

Why are these two parameters set here? Don't you? Thank you.
Some parameters are required when the program runs. For example, COPY command, you need to specify the source file and the target file name, you have to pass through argc and argv to transfer from: http://www.cnblogs.com/avril/archive/2010/03/22/1691477.html

Int main (INT argc, char * 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.