Main (INT argc, char * argv [])

Source: Internet
Author: User
Main (INT argc, char * argv [])
Original Author: syuui (syuui)
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

/* Argtest. C */
# 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

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.