默认INT主要论据在C / C ++ [英] Default int main arguments in C/C++
问题描述
我是用C / C ++项目乱搞,我注意到了这一点:
I was messing around with projects in C/C++ and I noticed this:
C ++
#include <iostream.h>
int main (int argc, const char * argv[]) {
// insert code here...
cout << "Hello, World!\n";
return 0;
}
和
C
#include <stdio.h>
int main (int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
return 0;
}
所以我总是那种想知道这一点,究竟是什么做的那些默认参数用C做/ C ++下INT主?我知道,应用程序仍然会编译没有他们,但他们所服务的目的是什么?
So I've always sort of wondered about this, what exactly do those default arguments do in C/C++ under int main? I know that the application will still compile without them, but what purpose do they serve?
推荐答案
他们认为传递到命令行程序的参数。举例来说,如果我有计划的a.out
和我正是如此调用它:
They hold the arguments passed to the program on the command line. For example, if I have program a.out
and I invoke it thusly:
$ ./a.out arg1 arg2
的argv
的内容将是包含字符串数组
The contents of argv
will be an array of strings containing
- [0]
的a.out
- 可执行文件的文件名始终是第一要素 - [1]
ARG1
- 其他参数 - [2]
ARG2
- 我通过
- [0]
"a.out"
- The executable's file name is always the first element - [1]
"arg1"
- The other arguments - [2]
"arg2"
- that I passed
ARGC
持有的argv
元素的数量(在C你需要另一个变量知道有多少元素有在一个数组,当传递给函数)。
argc
holds the number of elements in argv
(as in C you need another variable to know how many elements there are in an array, when passed to a function).
您可以用这个简单的程序自己尝试一下:
You can try it yourself with this simple program:
#include <stdio.h>
int main(int argc, char ** argv){
int i;
for(i = 0; i < argc; i++){
printf("Argument %i = %s\n", i, argv[i]);
}
return 0;
}
这篇关于默认INT主要论据在C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!