简体   繁体   English

int argc, char *argv[] 是什么意思?

[英]What does int argc, char *argv[] mean?

In many C++ IDE's and compilers, when it generates the main function for you, it looks like this:在许多 C++ IDE 和编译器中,当它为您生成主要的 function 时,它看起来像这样:

int main(int argc, char *argv[])

When I code C++ without an IDE, just with a command line compiler, I type:当我在没有 IDE 的情况下编写 C++ 时,仅使用命令行编译器,我输入:

int main()

without any parameters.没有任何参数。 What does this mean, and is it vital to my program?这是什么意思,对我的计划至关重要吗?

argv and argc are how command line arguments are passed to main() in C and C++. argvargc是命令行参数在 C 和 C++ 中传递给main()的方式。

argc will be the number of strings pointed to by argv . argc将是argv指向的字符串数。 This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.这将(实际上)是 1 加上参数的数量,因为几乎所有的实现都会将程序的名称添加到数组中。

The variables are named argc ( argument count ) and argv ( argument vector ) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings) is equally valid.变量按约定命名为argc参数计数)和argv参数向量),但可以为它们指定任何有效标识符: int main(int num_args, char** arg_strings)同样有效。

They can also be omitted entirely, yielding int main() , if you do not intend to process command line arguments.如果您不打算处理命令行参数,它们也可以完全省略,产生int main()

Try the following program:试试下面的程序:

#include <iostream>

int main(int argc, char** argv) {
    std::cout << "Have " << argc << " arguments:" << std::endl;
    for (int i = 0; i < argc; ++i) {
        std::cout << argv[i] << std::endl;
    }
}

Running it with ./test a1 b2 c3 will output使用./test a1 b2 c3运行它会输出

Have 4 arguments:
./test
a1
b2
c3

argc is the number of arguments being passed into your program from the command line and argv is the array of arguments. argc是从命令行传递到程序中的参数数量,而argv是参数数组。

You can loop through the arguments knowing the number of them like:您可以遍历参数,知道它们的数量,例如:

for(int i = 0; i < argc; i++)
{
    // argv[i] is the argument at index i
}

Suppose you run your program thus (using sh syntax):假设您这样运行程序(使用sh语法):

myprog arg1 arg2 'arg 3'

If you declared your main as int main(int argc, char *argv[]) , then (in most environments), your main() will be called as if like:如果您将 main 声明为int main(int argc, char *argv[]) ,那么(在大多数环境中),您的main()将被称为:

p = { "myprog", "arg1", "arg2", "arg 3", NULL };
exit(main(4, p));

However, if you declared your main as int main() , it will be called something like但是,如果您将 main 声明为int main() ,它将被称为类似

exit(main());

and you don't get the arguments passed.你没有得到通过的论点。

Two additional things to note:还有两点需要注意:

  1. These are the only two standard-mandated signatures for main .这是main仅有的两个标准强制签名。 If a particular platform accepts extra arguments or a different return type, then that's an extension and should not be relied upon in a portable program.如果特定平台接受额外的参数或不同的返回类型,那么这是一个扩展,不应该在可移植程序中依赖。
  2. *argv[] and **argv are exactly equivalent, so you can write int main(int argc, char *argv[]) as int main(int argc, char **argv) . *argv[]**argv完全等价,因此您可以将int main(int argc, char *argv[])写为int main(int argc, char **argv)
int main();

This is a simple declaration.这是一个简单的声明。 It cannot take any command line arguments.它不能接受任何命令行参数。

int main(int argc, char* argv[]);

This declaration is used when your program must take command-line arguments.当您的程序必须采用命令行参数时使用此声明。 When run like such:像这样运行时:

myprogram arg1 arg2 arg3

argc , or Argument Count, will be set to 4 (four arguments), and argv , or Argument Vectors, will be populated with string pointers to "myprogram", "arg1", "arg2", and "arg3". argc或 Argument Count 将设置为 4(四个参数),并且argv或 Argument Vectors 将填充指向“myprogram”、“arg1”、“arg2”和“arg3”的字符串指针。 The program invocation ( myprogram ) is included in the arguments!程序调用( myprogram )包含在参数中!

Alternatively, you could use:或者,您可以使用:

int main(int argc, char** argv);

This is also valid.这也是有效的。

There is another parameter you can add:您可以添加另一个参数:

int main (int argc, char *argv[], char *envp[])

The envp parameter also contains environment variables. envp参数还包含环境变量。 Each entry follows this format:每个条目都遵循以下格式:

VARIABLENAME=VariableValue

like this:像这样:

SHELL=/bin/bash    

The environment variables list is null-terminated.环境变量列表以空值结尾。

IMPORTANT: DO NOT use any argv or envp values directly in calls to system() !重要提示:不要在调用system()时直接使用任何argvenvp值! This is a huge security hole as malicious users could set environment variables to command-line commands and (potentially) cause massive damage.这是一个巨大的安全漏洞,因为恶意用户可以将环境变量设置为命令行命令并(可能)造成巨大破坏。 In general, just don't use system() .一般来说,不要使用system() There is almost always a better solution implemented through C libraries.几乎总是有通过 C 库实现的更好的解决方案。

The parameters to main represent the command line parameters provided to the program when it was started. main的参数表示程序启动时提供给程序的命令行参数。 The argc parameter represents the number of command line arguments, and char *argv[] is an array of strings (character pointers) representing the individual arguments provided on the command line. argc参数表示命令行参数的数量, char *argv[]是一个字符串数组(字符指针),表示命令行上提供的各个参数。

The main function can have two parameters, argc and argv . main函数可以有两个参数, argcargv argc is an integer ( int ) parameter, and it is the number of arguments passed to the program. argc是一个整数 ( int ) 参数,它是传递给程序的参数数量。

The program name is always the first argument, so there will be at least one argument to a program and the minimum value of argc will be one.程序名称始终是第一个参数,因此程序至少有一个参数,而argc的最小值将是一个。 But if a program has itself two arguments the value of argc will be three.但是如果一个程序本身有两个参数,则argc的值将是三个。

Parameter argv points to a string array and is called the argument vector .参数argv指向一个字符串数组,称为参数向量 It is a one dimensional string array of function arguments.它是函数参数的一维字符串数组。

第一个参数是提供的参数数量,第二个参数是表示这些参数的字符串列表。

Lets consider the declaration:让我们考虑一下声明:

int main (int argc, char *argv[])

In the above declaration, the type of the second parameter named argv is actually a char** .在上面的声明中,名为argv的第二个参数的类型实际上是一个char** That is, argv is a pointer to a pointer to a char .也就是说, argv是一个指向char的指针 This is because a char* [] decays to a char** due to type decay .这是因为char* []由于类型衰减衰减char** For example, the below given declarations are equivalent:例如,下面给出的声明是等价的:

int main (int argc, char *argv[]); //first declaration
int main (int argc, char **argv);  //RE-DECLARATION. Equivalent to the above declaration

In other words, argv is a pointer that points to the first element of an array with elements of type char* .换句话说, argv是一个指针,它指向具有char*类型元素的数组的第一个元素。 Moreover, each elements argv[i] of the array(with elements of type char* ) itself point to a character which is the start of a null terminated character string.此外,数组的每个元素argv[i] (具有char*类型的元素)本身都指向一个字符,该字符是空终止字符串的开始。 That is, each element argv[i] points to the first element of an array with elements of type char (and not const char ).也就是说,每个元素argv[i]指向数组的第一个元素,其中元素类型为char (而不是const char )。 A diagram is given for illustration purposes:为说明目的给出了一个图表:

argv 和 argc

As already said in other answers, this form of declaration of main is used when we want to make use of the command line argument(s).正如在其他答案中已经说过的,当我们想要使用命令行参数时,会使用这种形式的main声明。

Both of两者的

int main(int argc, char *argv[]);
int main();

are legal definitions of the entry point for a C or C++ program.是 C 或 C++ 程序入口点的合法定义。 Stroustrup: C++ Style and Technique FAQ details some of the variations that are possible or legal for your main function. Stroustrup:C++ 风格和技术常见问题解答详细介绍了一些对您的主要功能可能或合法的变体。

In case you learn something from this如果你从中学到一些东西

#include<iostream>
using namespace std;
int main(int argc, char** argv) {
   cout << "This program has " << argc << " arguments:" << endl;
   for (int i = 0; i < argc; ++i) {
      cout << argv[i] << endl;
   }
   return 0;
}

This program has 3 arguments.该程序有 3 个 arguments。 Then the output will be like this.那么output就会是这个样子。

C:\Users\user\Desktop\hello.exe
hello
people

When using int and char** , the first argument will be the number of commands in by which the programs is called and second one is all those commands使用intchar**时,第一个参数将是调用程序的命令数,第二个参数是所有这些命令

Just to add because someone says there is a third parameter ( *envp[] ), it's true, there is, but is not POSIX safe, if you want your program to use environment variables you should use extern char environ;D只是补充一下,因为有人说有第三个参数( *envp[] ),这是真的,有,但不是 POSIX 安全的,如果你希望你的程序使用环境变量,你应该使用extern char environ;D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM