The meanings and usages of parameters argc and argv in C + + main function

Source: Internet
Author: User
Tags naming convention

Original address: 51987413

ARGC is the abbreviation for argument count, which indicates the number of arguments passed into the main function;

ARGV is an abbreviation for the argument vector, indicating that the parameter sequence or pointer passed in the main function, and that the first parameter argv[0] must be the name of the program and contains the full path of the program. So exactly the number of arguments that need to be entered for the main function should be argc-1;

Simple usage example, new project key code:

#include <iostream> using namespace std;void main (int Argc,char *argv[]) {for (int i=0;i<argc;i++) {cout<< "argument[" <<i<< "is:" <<argv[i]<<endl;} System ("pause"); }

ARGV is a pointer to a pointer, and the second argument of the main function "char *argv[]" can also be replaced with "Char **ARGV", which is equivalent.

Press F5 to run in the compiled environment, the output is as follows:

It can be seen that the first variable argv[0] that holds the name of the program is still present in the case where no arguments are passed in.

There are two ways to pass parameters to the main function, the first way is to set in the compilation environment, take vs2012 as an example, right-click the configuration Properties, properties-------------parameters, and enter in the command arguments, separated by a space between each parameter.

Then click OK and apply, and the following is displayed after the run:

The second approach is also often used in a way that is passed through the command prompt. First, you need to open a command Prompt window, click on the Start menu in "Search programs and Files" to enter the command "CMD" or directly press the shortcut key Windows+r, in the popup dialog box to enter "CMD" to open the command Prompt window:

After opening the command Prompt window, you need to enter the full path of the generated EXE file, an easy way is to drag the exe file directly into the prompt window, then input parameters, separated by a space, followed by a carriage return, shown as follows:

If you insist on entering the full path manually, you will find that waiting for your "CTRL + C" path, in the prompt window to press "CTRL + V" but not paste, this time you can right-click in the window to try, you will find the right menu paste function is still valid.

The next example shows a picture using OpenCV:

#include <iostream><core/core.hpp>usingnamespace   Usingnamespace  cv;   void Main (int argc,Char * *argv) {Mat image=imread (argv[1] ); Imshow ("Lena", image); Waitkey (); }

Note that the read-in parameter is argv[1], which is run at the Command Prompt window:

Finally, the general compiler defaults to using ARGC and argv two names as arguments to the main function, but these two parameters are not required to be named, and you can use any variable name that conforms to the C + + language naming convention as the entry parameter, the effect is the same:

#include <iostream>#include<core/core.hpp>#includeusing namespacestd;using namespaceCV;voidMainintValueChar**Point ) {  for(intI=0; i<value;i++) {cout<<"argument["<<i<<"] is:"<<point[i]<<Endl;} System ("Pause"); } 

(go) The meanings and usages of parameters argc and argv in C + + main function

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.