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

查看:73
本文介绍了int main(int argc,char * argv [])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个:

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

在体内,有时您可以使用argv[1]查找程序.

In the body, you can sometimes find programs using argv[1].

我们何时在argv[0]上使用argv[1]?仅仅是当我们只想在命令行中读取 second 参数时?

When do we use argv[1] over argv[0]? Is it only when we just want to read the second argument in the command line?

推荐答案

按照惯例argv[0]当前程序的名称(或路径),而argv[argc - 1]是用户提供的命令行参数.

By convention, argv[0] is the current program's name (or path), and argv[1] through argv[argc - 1] are the command-line arguments that the user provides.

但是,这不是必须是正确的-程序可以使用特定于OS的功能来绕过此要求,并且这种情况经常发生,您应该意识到这一点. (尽管不确定,我不确定是否可以做很多事情……)

However, this doesn't have to be true -- programs can OS-specific functions to bypass this requirement, and this happens often enough that you should be aware of it. (I'm not sure if there's much you can do even if you're aware of it, though...)

示例:

gcc -O3 -o temp.o "My file.c"

(应该)产生以下参数:

would (should) produce the following arguments:

argc: 5
argv: ["gcc", "-O3", "-o", "temp.o", "My file.c"]

所以说argv[0]是指gcc,而不是 -O3.

So saying argv[0] would refer to gcc, not to -O3.

这篇关于int main(int argc,char * argv [])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆