C/C ++ learning notes on addresses, pointers, and reference variables (8): command line parameters

Source: Internet
Author: User

This article describes the command line parameters: argc and argv.

C ++ProgramThe main function can contain two parameters. These two parameters are respectively an integer and an array pointing to a pointer of the struct type. Integer Parameters record the number of command line parameters that you enter in the command line of the program. The char * [] character pointer array points to each parameter in the command line. Although the two parameters can be named with any identifier, it is customary to always use argc and argv.

They have never been defined in our previous programs.

Note: The main function does not define the parameters. It is also the only function that does not need to define the parameters.

 

The argc parameter must be at least 1. If you enter a parameter in the command line, it is larger than 1. Argv points to at least one struct pointer in the array, that is, argv [0]. It usually points to the executable file name of the program, and some versions also include the path of the program.

ExampleCodeAs follows:

#include <iostream> </P> <p> // /// // <br //> // The main () function. <br/> //////////////////////////////////// //// <br/> int main (INT argc, char * argv []) <br/>{< br/> STD: cout <"this program is" <argv [0] <STD: Endl; <br/> for (INT Arg = 1; arg <argc; arg ++) <br/> STD: cout <"argument" <Arg <": "<br/> <argv [Arg] <STD: Endl; </P> <p> return 0; <br/>}

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.