C++ int main(int argc, char **argv)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25333364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
int main(int argc, char **argv)
提问by Murilo
I am beginner in C++ and I am used to code with int main(), and now I am working with :
我是 C++ 初学者,习惯于使用 int main() 进行编码,现在我正在使用:
int main(int argc, char **argv)
int main(int argc, char **argv)
And I don't know exactly what this line of code means. So, I looked up for some answer in the internet and I found this block of code:
而且我不知道这行代码到底是什么意思。因此,我在互联网上查找了一些答案,并找到了以下代码块:
std::cout << "Have " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
Which shows me the arguments that I have.
这向我展示了我的论点。
I run the application and the console shows:
我运行应用程序,控制台显示:
Have 1 arguments:
C:\Users\user\Documents\C++ Projects\Test\bin\Debug\Test.exe
有 1 个参数:
C:\Users\user\Documents\C++ Projects\Test\bin\Debug\Test.exe
And then it closes the application, because after this loop I have an if else statment based on the argc value.
然后它关闭应用程序,因为在这个循环之后我有一个基于 argc 值的 if else 语句。
So if argc is different from 3 it runs a exit(0)
.
因此,如果 argc 与 3 不同,它将运行exit(0)
.
My questions are:
我的问题是:
Why my argument is just my own application located on debug path ?
为什么我的论点只是位于调试路径上的我自己的应用程序?
And how can I get more than one argument ?
我怎样才能得到不止一个论点?
采纳答案by clcto
If you are running from Visual Studio, you can add arguments in the Project Properties. Right click the project that is running, select Properties. In the Debug tab, there is a input box for Command Line Arguments. Enter them there.
如果您从 Visual Studio 运行,则可以在项目属性中添加参数。右键单击正在运行的项目,选择属性。在调试选项卡中,有一个命令行参数输入框。在那里输入它们。
For codeblocks, see here.
有关代码块,请参见此处。
回答by jh314
argc
is the number of arguments used to run your program
argc
是用于运行程序的参数数量
argv
is an array of char*
arguments
argv
是一个char*
参数数组
argv[0]
is the name of the executable (in your case, it is Test.exe
)
argv[0]
是可执行文件的名称(在您的情况下,它是Test.exe
)
argv[1]
is the first argument that you pass in (if you passed in any).
argv[1]
是您传入的第一个参数(如果您传入任何参数)。
So if you run your program as Test.exe a b
, then argc
will be 3, and the contents of argv
will be:
因此,如果您将程序作为 运行Test.exe a b
,那么argc
将是 3,并且其内容argv
将是:
argv[0]
is Test.exe
argv[0]
是 Test.exe
argv[1]
is a
argv[1]
是 a
argv[2]
is b
argv[2]
是 b
回答by Basile Starynkevitch
You might try to run your application in a terminal or in command line, perhaps
您可能会尝试在终端或命令行中运行您的应用程序,也许
.\Test.exe one two
Then argc
should be 3, with argv[0]
being perhaps .\Test.exe
, and argv[2]
being two
. I leave you to experiment what argv[1]
is by using your favorite debugger.
然后argc
应该是 3,argv[0]
可能是.\Test.exe
,并且argv[2]
是two
。我让您argv[1]
通过使用您最喜欢的调试器来试验什么。
(actually I don't know Windows, but I am transposing what I know on Linux to your proprietary Microsoft operating system)
(实际上我不知道 Windows,但我正在将我在 Linux 上的知识转换为您专有的 Microsoft 操作系统)
Notice that on Linux or Posix system if you use globbingon the command line like
请注意,在 Linux 或 Posix 系统上,如果您在命令行上使用globbing,例如
/bin/ls -l a*
it is the invoking shellwhich expands the a*
to a sequence of words, and that expandedsequence is passed by execve(2)to the /bin/ls
executable.
它是调用shell将 展开a*
为一个单词序列,并且该扩展序列由execve(2)传递给/bin/ls
可执行文件。
Rumors say that on Windows that is not the case. Some Microsoft equivalent of Crt0might do the globbing expansion. Read Microsoft documention.
有传言说,在 Windows 上情况并非如此。某些 Microsoft 等效的Crt0可能会进行全局扩展。阅读微软文档。
AFAIK, it is guaranteed (at least on Linux and Posix systems) that argv[argc]
is the NULL
pointer.
AFAIK,可以保证(至少在 Linux 和 Posix 系统上)argv[argc]
是NULL
指针。
回答by zhirzh
argc
stands for argument count
- argc stores in itself total number of arguments passed during runtime
argv
stands for argument value
- argv stores in itself the arguments itself.
argc
代表argument count
- argc 在自身中存储运行时传递的参数总数
argv
代表argument value
- argv 在自身中存储参数本身。
first argument argv[0]
is the absolute path of your (executable)file
第一个参数argv[0]
是(可执行)文件的绝对路径
after that, every argv[n]
(n < argc) will have one argument, stored as a char*
string (even if you pass a number)
之后,每个argv[n]
(n < argc) 将有一个参数,存储为char*
字符串(即使您传递一个数字)
That'd be all.
这就是全部。
回答by yizzlez
int argc, char **argv
represent the command line arguments. By that I mean, if you run the application through cmd/terminal:
int argc, char **argv
代表命令行参数。我的意思是,如果您通过 cmd/终端运行应用程序:
>mypath\test.exe a b c d
^^ these arguments
argv[0]
is the always the path of the executable. argc
is the number of arguments.
argv[0]
始终是可执行文件的路径。argc
是参数的数量。
回答by π?ντα ?ε?
The 1st argument is always the program name called, the further ones are the arguments passed on the command line.
In other words the argv
replicates the complete command line called:
第一个参数始终是调用的程序名称,后面的参数是在命令行上传递的参数。
换句话说,argv
复制了完整的命令行,称为:
myprog arg1 arg2
The important thing to notice for the 1st argument, is that it can be used to distinguish certainly different behavior of the main program, depending on the program name (that can differ by e.g. using symbolic links). A good sample is how gzip
and gunzip
works.
第一个参数要注意的重要一点是,它可以用来区分主程序的不同行为,这取决于程序名称(可以通过例如使用符号链接而有所不同)。一个很好的例子是如何gzip
和gunzip
工作。