Talk C chestnuts together (57th back: C language instance -- parameters of the main function)

Source: Internet
Author: User

Talk C chestnuts together (57th back: C language instance -- parameters of the main function)

 

Hello, everyone. We talked about the Shortest Path of the graph in the last time. The example here is the parameter of the main function.

Number. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

 

When writing a program, we always write a main function, that is, the Main function. Normally, we do not

Write parameters, the most common syntax is:

Int main ()

{

Function body...

}

 

In fact, the main function has parameters. Its standard function prototype is:

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

 

{

Function body...

}

 

From the function prototype, we can see that the main function has two parameters:

The first parameter is argc, which indicates the number of parameters passed to the main function. The second parameter is argv [], which is an array. Each element in the array represents the specific content passed to the main function.

 

The following is an example:

Cd/home/usr

This command is a commonly used command in Mint. It indicates switching from the current directory to the/home/usr directory.

We all know that the cd command is run based on the cd program. The cd program contains the main function.

When executing the "cd/home/usr" command, the parameters passed to the main function are "cd" and "/home/usr ".

 

In this case, the value of argc is 2, indicating that two parameters of the main function are passed.

The array argv has two elements. The value of one element is "cd" and the value of the other element is "/home/usr ".

 

After understanding this principle, we can write a function to output the number and content of parameters passed to the main function. below is

For the program running result, refer:

 

./S-g-o parm aa. c bb. h 12345 // s is the program name. Similar to the cd in the example, the following content is the parameter

The count of param is: 7 // display The number of parameters passed to The main function

Param: 1 length: 3, -->./s

Param: 2 length: 2, -->-g

Param: 3 length: 2, -->-o

Param: 4 length: 4, --> parm

Param: 5 length: 4, --> aa. c

Param: 6 length: 4, --> bb. h

Param: 7 length: 5, --> 12345

 

The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it.

 

Let's talk about the example of main function parameters. I want to know what examples will be provided later, and I will try again.

 

 

Related Article

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.