ARGC is the total number of arguments in the command line
Argv[] is a argc parameter, where the No. 0 parameter is the full name of the program, and subsequent arguments
The user-entered parameters followed by the command line, such as:
int main (int argc, char* argv[])
{
int i;
for (i = 0; i<argc; i++)
cout<<argv[i]<<endl;
cin>>i;
return 0;
}
Typing at execution time
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 an array of characters whose size is int argc, which is used primarily for command-line arguments argv[] parameters, and each element in the array represents a parameter;
For example, you enter
Test A.C B.C t.c
The
ARGC = 4
ARGV[0] = "Test"
ARGV[1] = "A.C"
ARGV[2] = "B.C"
ARGV[3] = "T.C"
--------------------------------------------------------------------------------------------
ARGC records the number of parameters entered by the user on the command line that runs the program.
Arg[] There is at least one character pointer in the array pointed to, which is arg[0]. He usually points to the file name of the executable file in the program. Programs are also included in some versions of the compiler
The path where the file resides.
-------------------------------------------------------------------------
When you invoke an executable program, you need to pass parameters to the program in some cases. If we can type Notepad.exe in the console,
The Notepad program executes after the carriage return. If we want to open a text file at the same time when Notepad is opened, you can notepad.exe
Follow the path and name of the file, such as Notepad.exe Example.txt (the file is in the current path).
So how can you get these input parameters in the program? This work is done by the compiler, and the compiler will enter the information for the parameter
Into the argument list of the main function.
The argument list of the main function saves the input parameter information, the first parameter argc the number of input parameters,
The second argument is an array of strings, and each cell of the string array is of type char*, pointing to a C-style string.
Taking Notepad.exe Example.txt as an example
ARGC is 2, which means there are two valid units in the ARGV array
The string that the first cell points to is "notepad.exe"
The second cell points to a string that is "Example.txt"
The string that the first cell in the argv array points to is always the name of the executable, and the string to which the subsequent cell points is the argument that the program called.
The assignment process is done by the compiler, and we just need to read the data.
-----------------------------------------------------------------------------
int main (int argc, char *argv[], char *envp[])
The main () function is usually in int or void form. I prefer to define main with the int type. Because it can be returned to the operating system at the end of a value to indicate the performance.
int ARGC
This is used to indicate how many arguments you have when you enter a command at the command line. For example, after your program compiles, the executable file is Test.exe
D:\tc2>test
At this time, the value of ARGC is 1.
But
D:\tc2>test.exe Myarg1 MYARG2
, the value of ARGC is 3. That is, the command name plus two parameters, total three parameters
Char *argv[]
This stuff is used to get the parameters you've entered.
D:\tc2>test
At this time, the value of ARGC is 1,argv[0] is "test"
D:\tc2>test Myarg1 MYARG2
At this time, the value of ARGC is 3,argc[0] is "test", the value of argc[1] is "MYARG1", and the value of argc[2] is "myarg2".
This is generally used to provide very important information for the program, such as: Data file name, and so on.
Example: Copy A.C B.txt
At this time, A.C and B.txt are the so-called "very important messages". Without specifying these two files, you cannot copy them.
When your program uses the ARGC and argv parameters, you can simply determine the value of the ARGC to see if the program parameters meet the requirements
Char *envp[]
This stuff is relatively small in comparison. It is used to obtain the environment variables of the system.
For example: Under DOS, there is a path variable. When you enter a command at the DOS prompt (of course, this command is not an internal command of dir), DOS will first find the execution file of the command in the current directory. If it is not found, the path defined by path is searched, the execution is found, and the return bad command or file name is missing.
Type set at the DOS command prompt to view environment variables for the system
Similarly, under UNIX or Linux, there are system environment variables that are used more than DOS. such as commonly used $path, $USER, $HOME and so on.
ENVP Save all environment variables. Its format is (under Unix)
Path=/usr/bin;/local/bin;
Home=/home/shuui
That
environment variable name = value
DOS is probably the same.
Environment variables are typically used to provide additional information to a program. For example, you have made a program that displays the contents of the text. You want to control the number of characters displayed in a row. You can define an environment variable yourself (under Unix)
%setenv Number = 10
%echo $NUMBER
10
You can then read the environment variable in the program. It then determines how many characters are printed on a line based on its value. Thus, if you do not modify the environment variable, each time you execute the program, the number of characters displayed in a row is different
Here is an example program
#include <stdio.h>
int main (int argc, char *argv[], char *envp[])
{
int i;
printf ("You had 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]!= ' + '; i++)
{
printf ("%s\n", Envp[i]);
}
return 0;
}
D:\>argtest This is a test programe of main () ' s argments
You are inputed total 9 argments
Arg0:d:\tc\noname. Exe
Arg1:this
Arg2:is
Arg3:a
Arg4:test
Arg5:programe
Arg6:of
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
Cmdline=noname This is a test programe of main () ' s argments
-----------------------------------------------------------------------------------------
Command-line arguments AH. ARGC is the number of arguments, argv[] is a parameter, argv[0] is the file name, Argv[1] is the first parameter ...
If your EXE file name is: Myprog.exe, then
Myprog 12 22 32
Then argv[0]= "Myprog", argv[1]= "a", argv[2]= "22" ...
Exit () is the return code when the program exits. Can be received by other programs to determine whether the normal exit. If exit (-1) is considered abnormal exit.
int main (int argc,char* argv[]) detailed