本文主要是介绍int main(int argc, char** argv),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
argc是命令行总的参数个数,是实际参数个数+1,因为argv[0]是编译后的可执行文件名
argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数为命令行后的用户输入参数。
假设程序的名称为prog,
1.当只输入prog,则由操作系统传来的参数为:
argc=1,表示只有一程序名称。
argc只有一个元素,argv[0]指向输入的程序路径及名称:./prog
2.当输入prog para_1,有一个参数,则由操作系统传来的参数为:
argc=2,表示除了程序名外还有一个参数。
argv[0]指向输入的程序路径及名称。
argv[1]指向参数para_1字符串。
3.当输入prog para_1 para_2 有2个参数,则由操作系统传来的参数为:
argc=3,表示除了程序名外还有2个参数。
argv[0]指向输入的程序路径及名称。
argv[1]指向参数para_1字符串。
argv[2]指向参数para_2字符串。
有四种写法:
int main()
int main(int argc) //不常用
int main(int argc, char** argv)
int main(int argc, char** argv, char** env)
例子:
//programname testmain1.exe
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv, char** env)
{
int i;
cout<< "These are the "<< argc <<" command- line arguments passed to main:"<<endl;
for(i=0; i<= argc ; i++)
cout<<"argv["<<i<<"]: "<<argv[i]<<endl;
cout<<"The environment strings on this system are: "<<endl;
for(i=0;env[i]!=NULL;i++)
cout<< "env{"<<i<<"]: "<<env[i]<<endl;
}
在cmd中运行testmain1.exe
f:\vs2010\testmain1\debug\testmain1 firt_argument 3 4
结果:
The arguments argc and argv of main is used as a way to send arguments to a program, the possibly most familiar way is to use the good ol' terminal where an user could type cat file. Here the word cat is a program that takes a file and outputs it to standard output (stdout).
The program receives the number of arguments in argc and the vector of arguments in argv, in the above the argument count would be two (The program name counts as the first argument) and theargument vector would contain [cat,file,null]. While the last element being a null-pointer.
Commonly, you would write like this:
int // Specifies that type of variable the function returns.// main() must return an integer
main ( int arc, char **argv ) {// codereturn 0; // Indicates that everything vent well.
}
If your program does not require any arguments, it is equally valid to write a main-function in the following fashion:
int main() {// codereturn 0; // Zero indicates success, while any // Non-Zero value indicates a failure/error
}
In the early versions of the C language, there was no int before main as this was implied. Today, this is considered to be an error.
On POSIX-compliant systems (and Windows), there exists the possibility to use a third parameter char **envp which contains a vector of the programs environment variables. Further variations of the argument list of the main function exists, but I will not detail it here since it is non-standard.
Also, the naming of the variables is a convention and has no actual meaning. It is always a good idea to adhere to this so that you do not confuse others, but i would be equally valid to define main as
int main(int c, char **v, char **e) {// codereturn 0;
}
And for your second question, there is several ways to send arguments to a program. I would recommend you to look at the exec*()family of functions which is POSIX-standard, but it is probablyeasier to just use system("command arg1 arg2").
Hope this helps.
这篇关于int main(int argc, char** argv)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!