About int main (int argc, char * argv []) Comments

Usually in the VS environment, there will always see the main function of these two parameters, today suddenly want to know the principles and the role of these two parameters, so check the following information. Taught really.

The following blog is to look at a large space for God in Baidu, the original link: http: //hi.baidu.com/sgglong70626/item/8881322b2dce21c1ee10f11e

argc command line is the total number of parameters   
the argv [] is a pointer to a string stored in the command-line parameters, wherein the first parameter is 0 the full name of the program, after the parameters for the command-line parameters followed by user input, argv parameters string pointer array, which is the command line, each element of each string (string processing parameters are by) the first address. The length of the array of pointers is the number of parameters argc. Initial array elements imparted automatically by the system. For example:   
int main (int argc, char * the argv [])   
{   
int I;   
for (I = 0; I <argc; I ++)   
COUT << the argv [I] << endl;   
CIN I >>;   
return 0;   
}   
knock performing   
F: \ mYDOCU ~ 1 \ TEMPCODE    \ D1 \ DEBUG \ D1.EXE aaaa bbb ccc ddd
output as follows:   
F.: \ mydocu. 1 ~ \ TEMPCODE \ Dl \ the DEBUG \ D1.EXE   
AAAA   
BBB   
CCC   
ddd   
--- -------------------------------------------------- ---------------
char * argv [] is an array of characters whose size is int argc, mainly used for command line arguments argv [] parameters, each array element represents a parameter ;
For example, you enter the   
test ac bc tc   
the   
argc =. 4   

the argv [0] = "Test"   
the argv [. 1] = "AC"   
the argv [2] = "BC"   
the argv [. 3] = "TC"
------- -------------------------------------------------- -----------------------------------  
argc record the parameters entered by the user at the command line to run the program in number.   
arg [] array pointed to at least one character pointer that arg [0]. He usually point to the file name of the executable file of the program. In some versions of the compiler also included in the program
path of the file is located.
-------------------------------------------------- -----------------------
when calling an executable program, you need to pass parameters to the program under certain circumstances. If we can type notepad.exe in the console,
after the execution Enter the Notepad program. If we want to open a text file at the same time open notepad, notepad.exe can  
keep up with the back of the file path and name, such as notepad.exe example.txt (in the current path to the file).   

How the program can get these input parameters? This work is to help us complete the compiler, the compiler of the input parameters of the information
Add to the list of parameters main function.   

main function parameter list of the information stored in the input parameters, the first parameter argc record the number of input parameters,   
the second parameter is an array of strings, each string array is a unit of type char *, pointing to a c-style string.   
In Example notepad.exe example.txt   
argc is 2, that is effective in the argv array has two units   
string pointed to by the first unit is "notepad.exe"   
character string pointed to by the second unit is "example.txt in"   

argv the first element in the array pointed to the string always executable name of the program, after the unit is in turn points to the string parameter when invoked.   

This assignment process is complete compiler, we need only read data on it.
-------------------------------------------------- ---------------------------

int main (int argc, char * the argv [], char * envp [])   
main () function is generally using int-shaped or void. I prefer to use the definition of type int main. Because at the end of the operating system can be returned to a value to represent the performance.   

int argc   
this stuff that you used to enter commands at the command line when there are a number of parameters. Let's say after your compiler executable file is test.exe   
D: \ TC2> the Test   
this time, the value of argc is 1   
but   
D: \ tc2> test.exe myarg1 myarg2   
, then the value of argc is 3. That is the command name with two parameters, a total of three parameters   

char * argv []   
This parameter is used to get stuff you enter   
D: \ tc2> test   
this time, the value of argc is 1, argv [0] of value is the "Test"   
D: \ TC2> Test myarg1 myarg2   
this time, the value of argc is 3, argc [0] value is "test", argc value [1] is "myarg1", argc [2] value is "myarg2".   
This stuff is generally used to provide very important information, such as a program: data file name, and so on.   
Such as: copy ac b.txt   
this time, ac and b.txt so-called "very important information." These two documents do not specify, you can not copy.   
When your program to use argc and argv these two parameters, simply by judging the value of argc, take a look at the parameters of the program meets the requirements   

char * envp []   
this stuff is relatively with less than. It is used to obtain the environment variable system.   
Such as: In DOS, there is a PATH variable. When you enter a command (of course, this is not the dir command a kind of internal order) when the DOS prompt, DOS will first look execute this command file in the current directory. If not, then the next path PATH defined look for, find the execution can not find the return Bad command or file name   
Type set to see the system in the DOS command prompt environment variables   
Likewise, under UNIX or LINUX, there are system environment variables, and to be more use than DOS. As usual $ PATH, $ USER, $ HOME and so on.   
envp save all the environmental variables. The format (UNIX under)   
the PATH = / usr / bin; / local / bin;   
the HOME = / Home / shuui   
namely:   
the environment variable name = value   
under DOS is probably the same.   
Environment variables are generally used to provide additional information for the program. For example, do you display the contents of a text of the program. You want to control the number of characters displayed in its row. You can define an environment variable (UNIX under) yourself   
% setenv NUMBER = 10   
% NUMBER echo $   
10   
and then you can read this environment variable in the program. Then decide how many characters a line of output based on its value. This way, if you do not modify environment variables, then every time you run this program, the number of characters displayed in one line is not the same   
Here is an example of program   

/ * argtest.c * /   
#include <stdio.h>   
int main ( argc int, char * the 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   :   a   
arg4   :   test   
arg5   :programs   
Arg6: of   
Arg7: main () 'S   
Arg8: argments   
of The Follow IS envp:   
the TMP = C: \ the WINDOWS \ the TEMP   
the TEMP = C: \ the WINDOWS \ the TEMP   
the PROMPT = $ P $ G   
WinBootDir = C: \ the WINDOWS   
the PATH = C: \ the WINDOWS; C: \ the WINDOWS \ the COMMAND   
the COMSPEC = C: \ the WINDOWS \ the COMMAND.COM   
SBPCI = C: \ SBPCI   
windir = C: \ the WINDOWS   
BLASTER = I7 Dl A220 P330 H7 T6   
CMDLINE = noname IS A Test programe functions of the this main ( ) 'S argments     
---------------------------------------------- -------------------------------------------
command line parameters ah. is the number of parameters argc, argv [] parameter, argv [0] is the file name, argv [1] is the first argument ...   
if you have exe file name: myprog.exe, then   
myprog 12 22 32   
Then the argv [0] = "the myprog", the argv [. 1] = "12 is", the argv [2] = "22 is" ...   

Exit () is the return code when the program exits. It can be received by other programs, to determine whether to exit properly. Such as exit (-1) considered abnormal exit.
-------------------------------------------------- -------------------------------------------

ask here why these two settings argument it? Do not do it? Thank you,
need to provide some parameters Sometimes the program is running. For example, the copy command, you need to specify the source and destination file name, you have to pass through argc and argv

 

1, int main (int argc, char * argv) and int main (int argc, char ** argv) difference?

     

main int (int argc, char * argv)

  argc: integer, to give a command when you run the program the number of statistical line arguments

  * argv: string used to store the string to point to your argument, each element points to a parameter

  argv [0] full path name pointing to a program running

  on a string of argv [1] points to the DOS command line execution program name

  argv [2] after the second point to executable name string

     

  int main (int argc, char ** argv ) corresponds to the main int (int argc, char * argv [])

     argc: integer, gave when you run the program used to count the number of command line arguments

  * argv: string used to store point your string parameter, each element pointing to a parameter

  argv [1] points to the DOS command line execution pointer to the first string of the program name

  argv execution of the program name after the point [2] two string pointer

      in int main (int argc, char ** argv) in argv [1] is a pointer to an address stored content, and the main int (int argc, char

       * the argv) in argv [1 ] is an array element (personal understanding)

 Our main function is often used without parameters. So parentheses after the main brackets are empty. In fact, the main function can take parameters, this parameter can be considered a form of argument to the main function. C language specified parameters can only have two main functions, it is customary to write these two parameters argc and argv. Thus, the main function of the first function can be written as: main (argc, argv) C language also provides argc (first parameter) must be an integer variable, the argv (second parameter) must be a pointer to a string array. After adding parameter description, the main function of the first function to be written as:  
main (argc, the argv)
int the argv;
char * the argv []; or written:
main (int argc, char * the argv [])
  Since the main function can not be other function call, it is impossible to obtain the actual value of the internal procedures. So, where the argument parameter value to the main function of it? In fact, the main function of the parameter value is obtained from the operating system command line. When we want to run an executable file, type the file name at the DOS prompt, then enter the actual parameters of these arguments can be transferred to the main parameter to go.

  The general form of the command-line DOS prompt is: C: \> executable file name Parameters ......; it should be particularly noted that, main two shape parameters and parameters in the command line
Position is not one to one. Because, the main parameter is only two, and not limitation, in principle the number of parameters on the command line. argc parameter indicates the number of command line arguments (note: the file name itself can be considered a parameter), the value of argc enter a command line in the system automatically according to the number of actual arguments given. For example, the command line: C: \> E6 24 BASIC dbase FORTRAN because the file name E6 24 itself can be considered a parameter, so a total of four parameters, so argc achieved a value of 4. argv array of pointers to parameter is a string, which is the command line, each element of each string (string processing parameters are by) the first address. The length of the pointer array is the number of parameters. Initial array elements imparted automatically by the system. Figure 6.8 indicates that:
main (int argc, char * the argv) {
the while (argc ->. 1)
the printf ( "% S \ n-", the argv * ++);
}
This example is a command line input If the executable file on a parameter named embodiment e24.exe, a is stored in the disk drive.
Therefore, command line input: C: \> a: e24   BASIC dBASE FORTRAN
run results:
the BASIC
dBASE
FORTRAN
  The bank has four arguments, executing the main, argc is the initial value of 4. argv The four elements are divided into first address four strings. While statement execution, each cycle argv value by one, when the stop cycles equal argv 1, a total of three cycles, thus a total of three output parameters. In the printf function, since the print item * ++ argv is incremented by 1 and then the first print, so that the first print character string BASIC argv [1] refers to. Second, after three cycles, two strings are respectively printed. The e24 parameter is the file name, you do not have to output.

  Command line parameters embodiment there are two, n is the value of the second parameter 20 inputted. * ++ argv in the program value of the string "20", then the loop control variable while statement, the even outputs 20 by the function "atoi" to change it as an integer.
#include "stdlib.h"
main (int argc, char * the argv []) {
int A = 0, n-;
n-= atoi (* ++ the argv);
the while (N--) the printf ( "% D", A ++ * 2);
}
  this program is started from the even outputs n 0. Pointer to a pointer variable if a pointer variable is stored in the address of another pointer variable, the pointer variable is called a pointer to a pointer variable.

  We have already introduced, accessed through a pointer variable called indirect access, between short visit. Since the pointer variable directly to variables, so called single-level visit between. If accessed by a pointer variable is a pointer variable between two or more stages constitute visit. In the C language program, a series of inter-visit limit is not clear, but the number of stages between visits when the solution is not easy to understand too much, but also prone to error, therefore, rarely more than between two visits. The general form of a pointer to the pointer variable is described:  
type specifier ** pointer variable name;  
example: int ** pp; pp represents a pointer variable that points to another pointer variable, and this variable is a pointer pointing to an integer quantity . Here is an example to illustrate this relationship.
main () {
int X, P *, ** PP;
X = 10;
P = X &;
PP = & P;
the printf ( "% D = X \ n-", PP **);
}  
  The example program is a pointer variable p, point to the integer x; pp is a pointer variable, which pointer variable p. Accessed through pp variable x is written ** pp. X is the final output of the program 10. By the above example, the reader can learn to use instructions and a pointer to a pointer variable.

  First, the following definitions describe the program pointer array initialization ps and made assignment. And it illustrates pps is a pointer to a pointer variable. In cycle 5, pps were made ps [0], ps [1 ], ps [2], ps [3], ps [4] address value (shown in Figure 6.10). Then you can find the string through these addresses.
main () {
static char * PS [] = { "the BASIC", "DBASE", "C", "the FORTRAN",
"the PASCAL"};
char ** PPS;
int I;
for (I = 0; I <. 5 ; I ++) {
PPS + PS = I;
the printf ( "% S \ n-", * PPS);
}
}
this program is programmed pointer to a pointer variable, and outputs a plurality of strings.

 

Original link: About int main (int argc, char * argv []) Comments - hust_archer - blog https://www.cnblogs.com/ruixingw/p/3705918.html Park

Guess you like

Origin www.cnblogs.com/tongongV/p/10961250.html