Parameters of main function in C language

Source: Internet
Author: User
Tags dbase

Transferred from: http://blog.csdn.net/cnctloveyu/article/details/3905720

The main function that we often use is without parameters. Therefore, the parentheses after main are empty brackets. In fact, the main function can take arguments, which can be thought of as the formal parameters of the main function. The C language stipulates that the main function can only have two parameters, which are used to write the two parameters argc and argv. Therefore, the function header of the main function can be written as: Main (ARGC,ARGV) C language also specifies that ARGC (the first parameter) must be an integer variable, argv (the second parameter) must be an array of pointers to the string. With the formal parameter description, the function head of the main function should be written as: Main (argc,argv) int argv; Char *argv[]; or written as: main (int argc,char *argv[])

Because the main function cannot be called by other functions, it is not possible to get the actual value inside the program. So where does the argument value be given to the formal parameter of the main function? In fact, the parameter value of the main function is obtained from the operating system command line.   When we want to run an executable file, type the file name at the DOS prompt, and then enter the actual parameters to transfer these arguments to the main parameter. The general form of the command line at the DOS prompt is:

c:/> executable file name parameter parameters ...;

However, it should be noted that the two parameters of main and the arguments in the command line are not one by one corresponding to the position. Because main has only two formal parameters, the number of arguments in the command line is not limited in principle. The ARGC parameter represents the number of arguments in the command line (note: The file name itself is also a parameter), and the value of ARGC is automatically assigned by the system by the number of actual parameters when the command lines are entered. For example, there are command behaviors:

C:/>e6_24 BASIC dbase FORTRAN, because the file name E6 24 itself is a parameter, so there are 4 parameters, so argc obtains a value of 4. The argv parameter is an array of string pointers whose element values are the first address of each string in the command line (arguments are processed by string). The length of the pointer array is the number of arguments. The initial value of an array element is automatically assigned by the system.

Main (int Argc,char *argv[]) {

while (argc-->1) printf ("%s/n", *++ARGV);

}

This example shows the parameters that are entered on the command line. If the executable file name of the above example is E24.exe, it is stored in the disk of drive A. So enter the command behavior:

C:/>a:e24 BASIC DBASE FORTRAN runs the result:

BASIC

DBASE

Fortran

There are 4 parameters in the row, and when you execute main, the initial value of the ARGC is 4. The 4 elements of the argv are divided into the first address of 4 strings. Execute the While statement, argv value minus 1 per loop once, when argv equals 1 o'clock stop loop, total three cycles, so three parameters can be output altogether. In the printf function, the string basic referred to by argv[1] is first printed because the printed item *++ARGV is first added 1 and then printed. The second to third cycle is printed with two strings respectively. The parameter E24 is a file name and does not have to be output

Parameters of main function in C language

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.