Use of the main function with parameters

Source: Internet
Author: User


previous contact with The main function is all without parameters. Therefore, the parentheses after main are empty brackets.

Main () {...}


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)


The C language also specifies that ARGC (the first parameter) must be an integer variable, and argv (the second parameter) must be an array of pointers to the string. With the formal parameter description, the function header of the main function should be 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.

The two parameters of main and the arguments in the command line are not one by one corresponding in 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.

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.

Argc->ac,argv->av

#include <stdio.h>//test.c int main (int ac, char *av[]) {int i;      printf ("%3d", AC);   AC record the number of parameters, the file name is also counted for (i=0;i<ac;i++) printf ("%3s", *av++); Output AC Parameters}
$ gcc-o test.c test$./test AA bb cc 4./test AA BB cc


To output only parameters, you can modify the for loop as follows,

for (i=1;i<ac;i++) printf ("%3s", *++av);


This article is from the "Dance Fish" blog, please be sure to keep this source http://ty1992.blog.51cto.com/7098269/1685069

Use of the main function with parameters

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.