C ++ program 1. main (int argc, char * argv []) Parameters Function Meaning

#include "iostream"
using namespace std;
int main(int argc, char *argv[])
{
	int i;
	for (i = 0; i < argc; i++)
	{
		cout << "argument" << i << ": " << argv[i] << endl;
	}
	cout << "total argument:" << argc;
	return EXIT_SUCCESS;
}

The row over the implementation of the program a total of five arguments, executing the main, argc is the initial value of 5. 5 elements argv into the first address five strings. For statement execution, each cycle i value by 1, i.e., when i is equal to 5 argc stop cycles, a total of 5 cycles, thus outputting a total of five parameters. In cout, since the argv print item, so that the first print argv [0] string referring to the program name. After a few cycles, respectively, after a few strings to print.

Can be concluded that: the first two parameters representing the input parameter argc Dos in the command line of the program name and the number of parameters, the second parameter argv [0] recorded program name, the latter argv [ i] input parameters recorded.

In addition argc argv is an identifier, you can change the name.

 int main (), int main (int argc, char ** argv), int main (int argc, char * argv []) a total of three written

int main () is a way to write the main function of the default parameters, but also the way to write when I was learning C ++ is mainly used, and he is very used to such an approach.

int main (int argc, char ** argv) and int main (int argc, char * argv []) is the same effects and actions. Wherein argc compiled program is run: 1 + the number of input parameters (due to the program's name, the program name can be considered a parameter). argv is a pointer to an array of these parameters.

The result is left not directly run the source line input parameters produced any results (i.e., the number of parameters of a program name) on the command, the result is the right input on the command line abcd follows (with the spaces between the spaced abcd): 1 selected click the attribute into 2, 3 in the input parameters to the (total number of parameters of the program is 1 + 4 = 5).

Parameters related to the main function, as described in the book:

When mian takes no parameters are empty parentheses after the main bracket. 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 of two parameters and command line parameters is not a one-on position of. 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: \> test abcd because the file name itself can be considered a test parameter, so a total of five parameters, so argc achieved a value of 5. 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.

 

发布了28 篇原创文章 · 获赞 4 · 访问量 1万+

Guess you like

Origin blog.csdn.net/m0_37957160/article/details/103819230