主(INT ARGC,CHAR *的argv []) [英] main(int argc, char *argv[])
问题描述
可能显示的文件:结果
哪些参数的main()呢?结果
是什么INT ARGC,CHAR *的argv []是什么意思?
每个程序都是始于主(INT ARGC,CHAR *的argv [])
定义。
我不明白这意味着什么。我将非常高兴,如果有人可以解释为什么我们使用这些参数如果我们不在程序中使用它们?为什么不干脆:<?code> INT的main()
Every program is starts with main(int argc, char *argv[])
definition .
I don't understand what it means. I would be very glad if somebody could explain why we use these arguments if we dont use them in the program? Why not just: int main()
?
编辑:
该计划的名称是的要素之一 *的argv []
和 ARGC
是数参数在数*的argv []
?什么是发送到其他参数*的argv []
?我们如何给他们?
The name of the program is one of the elements of *argv[]
and argc
is the count of the number of arguments in *argv[]
? What are the other arguments sent to *argv[]
? How do we send them?
推荐答案
ARGC
和的argv
的参数主
作为的方式发送参数的程序时,可能是最熟悉的方式是使用好醇'终端,其中一个用户可以键入<大骨节病> 猫文件
骨节病>。这里所说的猫
是一个程序,需要一个文件,并将其输出到标准输出(标准输出
)。
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
).
程序接收的参数个数在 ARGC
和参数在的argv
的载体,在上面 ARG 输稿 C 'mount将有两个(程序名称数作为第一个参数),并在 ARG 输稿 v 埃克特将包含[猫
,文件
,空的。而最后一个元素是一个空指针。
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 the argument vector would contain [cat
,file
,null]. While the last element being a null-pointer.
通常情况下,你会写这样的:
Commonly, you would write it like this:
int // Specifies that type of variable the function returns.
// main() must return an integer
main ( int argc, char **argv ) {
// code
return 0; // Indicates that everything went 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
}
在C语言的早期版本中,没有 INT
在主
因为这是暗示。今天,这被认为是一个错误。
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.
在 POSIX 兼容的系统(以及Windows)中,存在使用第三个参数的可能性的char ** envp
其中包含一个矢量节目 ENV ironment变量的。在主
函数的参数列表中的其他变体存在,但我不会详细在这里,因为它是非标准的。
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 it would be equally valid to define main
as
int main(int c, char **v, char **e) {
// code
return 0;
}
和关于第二个问题,有几种方法发送参数的程序。我建议你看看 EXEC *()
系列函数这就是 POSIX - 标准,但它可能是的更简单只是使用的 系统
(命令ARG1 ARG2)
,但使用系统()
通常在,因为它不能保证所有的系统上工作皱起了眉头。我还没有测试它自己;但如果没有庆典
,的zsh
,或者安装一个* NIX系统上的其他外壳,系统()
将失败。
And for your second question, there are 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 probably easier to just use system
("command arg1 arg2")
, but the use of system()
is usually frowned upon as it is not guaranteed to work on every system. I have not tested it myself; but if there is no bash
,zsh
, or other shell installed on a *NIX-system, system()
will fail.
这篇关于主(INT ARGC,CHAR *的argv [])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!