简体   繁体   English

在 c 中的 main() 中调用 main()

[英]calling main() in main() in c

是否可以在 c 中的main()函数中调用main()

是的,C 允许您调用 main 函数(而 C++ 不允许

It is indeed allowable to call main() from itself, even avoiding stack overflow with the same method used for any other recursive code, a terminating condition such as with:确实允许从自身调用main() ,甚至可以使用用于任何其他递归代码的相同方法避免堆栈溢出,终止条件例如:

#include <stdio.h>
int main (int argc, char *argv[]) {
    printf ("Running main with argc = %d, last = '%s'\n",
        argc, argv[argc-1]);
    if (argc > 1)
        return main(argc - 1, argv);
    return 0;
}

which, when run as testprog 1 2 3 , outputs:当作为testprog 1 2 3运行时,输出:

Running main with argc = 4, last = '3'
Running main with argc = 3, last = '2'
Running main with argc = 2, last = '1'
Running main with argc = 1, last = 'testprog'

However, since that's only anecdotal evidence, we should turn to the standard.然而,由于这只是轶事证据,我们应该转向标准。 ISO C11 section 4 Conformance states: ISO C11 第4 Conformance4 Conformance规定:

1/ In this International Standard, "shall" is to be interpreted as a requirement on an implementation or on a program; 1/ 在本国际标准中,“应”被解释为对实现或程序的要求; conversely, "shall not" is to be interpreted as a prohibition.相反,“不应”应解释为禁止。

2/ If a "shall" or "shall not" requirement that appears outside of a constraint or runtime-constraint is violated, the behavior is undefined. 2/ 如果违反约束或运行时约束之外的“应该”或“不应”要求,则行为未定义。 Undefined behavior is otherwise indicated in this International Standard by the words "undefined behavior" or by the omission of any explicit definition of behavior.在本国际标准中,未定义的行为用“未定义的行为”一词或省略任何明确的行为定义来表示。 There is no difference in emphasis among these three;这三者的侧重点没有区别; they all describe "behavior that is undefined".它们都描述了“未定义的行为”。

3/ A program that is correct in all other aspects, operating on correct data, containing unspecified behavior shall be a correct program and act in accordance with 5.1.2.3. 3/ 在所有其他方面都正确、对正确数据进行操作、包含未指定行为的程序应是正确的程序并按照 5.1.2.3 进行操作。

Now, since there's no explicit prohibition anywhere in the standard on main() calling itself, clause 3 above is the controlling aspect.现在,由于标准中的任何地方都没有明确禁止main()调用自身,因此上面的第 3 条是控制方面。

Further support can be seen in two places (my bold), first in section 5.1.2.2.3 Program termination :可以在两个地方(我的粗体)看到进一步的支持,首先在第5.1.2.2.3 Program termination5.1.2.2.3 Program termination

1/ If the return type of the main function is a type compatible with int , a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; 1/ 如果main函数的返回类型是与int兼容的类型,从初始调用到main函数的 return 等效于以main函数返回的值作为参数调用exit函数;

And then in section 7.21.3 Files :然后在第7.21.3 Files7.21.3 Files

5/ The file may be subsequently reopened, by the same or another program execution, and its contents reclaimed or modified (if it can be repositioned at its start). 5/ 文件可能随后被相同或另一个程序执行重新打开,并且其内容被回收或修改(如果它可以在其开始时重新定位)。 If the main function returns to its original caller, or if the exit function is called, all open files are closed (hence all output streams are flushed) before program termination.如果main函数返回到其原始调用者,或者如果调用exit函数,则在程序终止之前关闭所有打开的文件(因此刷新所有输出流)。

Both these sub-sections support the possibility that there may be other calls to main() over and above the initial/original one.这两个小节都支持在初始/原始调用之外可能还有其他main()调用的可能性。

Yes, we can call the main() within the main() function.是的,我们可以在 main() 函数中调用 main()。

The process of calling a function by the function itself is known as Recursion.通过函数本身调用函数的过程称为递归。

Well,you can call a main() within the main() function ,but you should have a condition that does not call the main() function to terminate the program.好吧,您可以在 main() 函数中调用 main() ,但是您应该有一个条件,即不调用 main() 函数来终止程序。

Otherwise,the program will never return and run infinitely.否则,程序永远不会返回并无限运行。

Yes you can .是的,你可以

Simple Program:简单程序:

int main()
{
    printf("Anything");
    main();
    return 0;
}

Explanation:解释:

A call stack or function stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing. call stackfunction stack用于多种相关目的,但使用call stackfunction stack的主要原因是跟踪每个活动子例程在完成执行时应返回控制的点。

A stack overflow occurs when too much memory is used on the call stack.当调用堆栈上使用了过多内存时,就会发生stack overflow

Here function main() is called repeatedly and its return address is stored in the stack.这里函数main()被重复调用,其返回地址存储在堆栈中。 After stack memory is full.堆栈内存满后。 It shows stack overflow error .它显示stack overflow error

是否可以在c中的main()函数内调用main()

是否可以在c中的main()函数内调用main()

是否可以在c中的main()函数内调用main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM