search
HomeCommon ProblemHow to use argc and argv

How to use argc and argv

argc is the abbreviation of argument count, indicating the number of parameters passed into the main function;

argv is the abbreviation of argument vector, indicating the number of parameters passed into the main function Parameter sequence or pointer, and the first parameter argv[0] must be the name of the program, and contains the complete path where the program is located, so to be precise, the number of parameters of the main function that we need to input should be argc-1; Spaces separate parameters, and their length is argc. The array subscript starts from 0, argv[argc]=NULL.

argv[0] points to the full path name of the program when it is run

argv[1] points to the first string after the program name when the program is executed in the command

argv [2] Points to the second string after the execution program name

argv[argc] is NULL

#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, the second parameter of the main function "char * argv[]" can also be replaced by "char **argv", the two are equivalent.

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

How to use argc and argv

Related information: "FAQ"

It can be seen that when no parameters are passed in, the first variable argv[0] that holds the program name still exists.

There are two ways to pass parameters to the main function. The first way is to set it in the compilation environment. Taking vs2012 as an example, right-click the project->Properties->Configuration Properties->Debug-> ;Command parameters, enter them in the command parameters, and separate each parameter with a space.

Then click OK and apply. After running, the display will be as follows:

How to use argc and argv

The second method, which is also often used, is to pass it in through the command prompt. . First, you need to open the command prompt window, click the start menu and enter the command "cmd" in "Search Programs and Files" or directly press the shortcut key Windows R, enter "cmd" in the pop-up dialog box to open the command prompt window:

How to use argc and argv

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, and then Enter the incoming parameters, separated by spaces, and then press Enter, as shown below:

How to use argc and argv

The above is the detailed content of How to use argc and argv. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)