The main function of C language to pass the parameter

Source: Internet
Author: User

1: Why do I need to pass a parameter to the main function

The first thing to understand is that it is not necessary to pass parameters to the Mian function, but sometimes we need to get different results by giving different arguments to the main function, for example, we want the value of a variable in the main function to be 0 o'clock to execute the child function A, the value of which is 1 o'clock to execute sub-function B, Then this time can be implemented by giving the main function, the Mian function parameter format is as follows:

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

Parameter explanation:

A parameter of type Argc:int that represents how many arguments are passed to the Mian function

argv a character array (or a double pointer), which is used to hold multiple strings, each of which is a parameter that we pass to the main function, and we need to be aware that we are./a.out when executing this program, it also passes a parameter to the main function.

2: Who is the main function to pass the argument

In the normal function A, the arguments inside it are passed by the function B that called function A, and likewise, the main function has a "function" called the main function to the main function, but we know that the main function is the entry function of a program, So to the main function is to call the program's program, that is, the main function of the parent process, and by the parent process to accept the main function return value.

The nature of the 3:main function's parameter transfer

program calls have various methods, but essentially the parent process fork a child process, and then the child process and a program bound to execute (EXEC function family), we can at the time of the exec to give him the same argument. A program call can be passed (that is, the main argument) is supported at the operating system level. As a simple example, when we enter the./a.out to execute a program, the essence is that the console process creates a child process to invoke us./a.out This executable program, and is responsible for the program's entry function is the main function of the parameter, and receive its return value.



This article is from the "11664570" blog, please be sure to keep this source http://11674570.blog.51cto.com/11664570/1870820

The main function of C language to pass the parameter

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.