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 ) {
     // code
     return 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() {
  // code
  return 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) {
   // code
   return 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.



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值