C/C ++ Command Line Parameter Parsing

Source: Internet
Author: User

 

In C/C ++, the main () function is used to transmit command line parameters.

 

To implement the command line parameters, we will define argc and argv in the form of main (INT argc, char * argv []), which can be replaced by your favorite name, not necessarily argv, argc is just a habit. char * argv [] is a pointer array. argv is a pointer array name. argv is not a constant pointer, it is a variable pointer with the characteristics of variables. It can be moved, and thus we can rewrite it to Char ** argv, the Int argc definition returns the number of parameters, so it is marked as an integer (INT ).

 

# Include <iostream. h>

Void main (INT argc, char ** argv)
{
Cout <"Command Line Parameter Parsing" <Endl;
Cout <"number of parameters:" <argc <Endl;
Cout <"output parameter value:" <Endl;
Int COUNT = 0;
While (count <argc) // while (argv! = NULL)
{
Cout <"no." <count + 1 <"parameter value:" <argv [count] <Endl;
Count ++;
}
Return;
}

To debug C/C ++ProgramEnter a B c d e in a variable line with a total of five parameters, separated by spaces.

 

 

Then run the program and the result is as follows:

 

The following six parameters are displayed: by default, the Directory of the executable file is set to the first value of the array argv. Therefore, there are a total of six parameter values.

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.