Drill down to the parameters of the main function argc,argv the use of the _c language

Source: Internet
Author: User
The main function in C + + language, often with parameter argc,argv, is as follows:
Copy Code code as follows:

int main (int argc, char** argv)

What are the roles of these two parameters? ARGC refers to the number of command line input parameters, argv stores all command-line arguments. If your program is hello.exe, if you run the program at the command line (you should first enter the directory of the Hello.exe file with a CD command at the command line), run the command:
Copy Code code as follows:

Hello.exe Shiqi Yu



The following program demonstrates the use of argc and argv:
Copy Code code as follows:

#include <stdio.h>
int main (int argc, char * * argv)
{
int i;
for (i=0 i < argc; i++)
printf ("Argument%d is%s.\n", I, argv[i]);
return 0;
}

If the above code is compiled to Hello.exe, then run:
Copy Code code as follows:

Hello.exe a b c d E

Will get
Copy Code code as follows:

Argument 0 is hello.exe.
Argument 1 is a.
Argument 2 is B.
Argument 3 is C.
Argument 4 is D.
Argument 5 is E.

Run:
Copy Code code as follows:

Hello.exe lena.jpg

Will get
Copy Code code as follows:

Argument 0 is hello.exe.
Argument 1 is lena.jpg.

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.