[英]A few questions about functions and arguments
So I'm learning about functions in a book.所以我正在学习一本书中的函数。
But why does the main
function works without a prototype?但是为什么
main
的 function 没有原型就可以工作呢?
I used to write main functions in my learning process like this:在我的学习过程中,我曾经写过这样的主要功能:
int main(void)
So it will not get any argument because of (void)
所以它不会因为
(void)
而得到任何争论
I tried to run my program with argument for example >./a.out 2
我尝试使用参数运行我的程序,例如
>./a.out 2
int main(int y){ printf("%s %d\n","y is",y); }
When I run it normally y
is 1, when run it with >./a.out 1
y
is 2, when there is more than one argument it increases by one.当我正常运行它时,
y
为 1,当使用>./a.out 1
运行它时, y
为 2,当有多个参数时,它增加一。 So it's not the right way but what causes this?所以这不是正确的方法,但是是什么原因造成的?
Declaring y
as char
says nothing so my guess is it works like the return value of scanf()
.将
y
声明为char
什么也没说,所以我的猜测是它的工作原理类似于scanf()
的返回值。 It returns number of successful inputs.它返回成功输入的数量。
A function must be either declared (ie a prototype) or defined before it is called. function 必须在调用之前声明(即原型)或定义。 The
main
function is different from other functions in that it's called by the program startup code and not some other function. main
的 function 与其他函数的不同之处在于它由程序启动代码调用,而不是其他一些 function。
There are however restrictions on what the signature of main
can be.然而,
main
的签名是有限制的。 On a regular hosted implementation, the C standard says it can be either:在常规托管实施中,C 标准说它可以是:
int main(void)
Or:或者:
int main(int argc, char **argv)
The latter case is used to read command line arguments.后一种情况用于读取命令行 arguments。
argc
contains the number of arguments passes, including the program name. argc
包含 arguments 遍数,包括程序名称。 argv
contains the actual arguments as an array of char *
. argv
包含实际的 arguments 作为char *
数组。
Many systems also support:许多系统还支持:
int main(int argc, char **argv, char **envp)
Where envp
contains the environment variables known to the program.其中
envp
包含程序已知的环境变量。
The prototype you're using: int main(int y)
is not supported in any implementation I'm aware of, so attempting to use such a prototype for main
invokes undefined behavior .您正在使用的原型:
int main(int y)
在我所知道的任何实现中都不受支持,因此尝试将这样的原型用于main
调用undefined behavior 。
When I run it normally
y
is 1, when run it with>./a.out 1
y
is 2, when there is more than one argument it increases by one.当我正常运行它时,
y
为 1,当使用>./a.out 1
运行它时,y
为 2,当有多个参数时,它增加一。 So it's not the right way but what causes this?所以这不是正确的方法,但是是什么原因造成的?
The standard entry for program startup kind of answers both your questions:程序启动的标准条目可以回答您的两个问题:
1 The function called at program startup is named
main
.1 程序启动时调用的 function 名为
main
。 The implementation declares no prototype for this function .该实现声明没有此 function 的原型。 It shall be defined with a return type of
int
and with no parameters:它应定义为返回类型为
int
且不带参数:int main(void) { /*... */ }
or with two parameters (referred to here as
argc
andargv
, though any names may be used , as they are local to the function in which they are declared):或使用两个参数(此处称为
argc
和argv
,但可以使用任何名称,因为它们是声明它们的 function 的本地名称):int main(int argc, char *argv[]) { /*... */ }
[...] [...]
As dbush already stated in the accepted answer these are the only two main
implementations allowed by the standard.正如dbush在接受的答案中已经说明的那样,这些是标准允许的仅有的两个
main
实现。
The standard leaves the responsability of dealing with undefined constructs opened and imposes no requirements for what the behavior should be, a given implementation may deal with the situation in any way it considers appropriate, this is known as undefined behavior .该标准保留了处理未定义构造的责任,并且没有对行为应该是什么强加任何要求,给定的实现可以以它认为合适的任何方式处理这种情况,这被称为未定义行为。
What seems to be happening is that your compiler is assuming that y
is argc
, which is allowed (as you can see in the second snippet of highlighted citation above), and argc
stores the number of arguments in the command line, which is consistent with the results you're having, but again, this behavior may differ in different compilers, systems or even versions of the same compiler.似乎正在发生的事情是您的编译器假设
y
是argc
,这是允许的(如您在上面突出显示的引用的第二个片段中所见),并且argc
将 arguments 的数量存储在命令行中,这与你得到的结果,但同样,这种行为在不同的编译器、系统甚至同一编译器的版本中可能会有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.