C语言 在 c 中的 main() 中调用 main()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4238179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:08:43  来源:igfitidea点击:

calling main() in main() in c

c

提问by anant

Is it possible to call main()within the main()function in c?

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

回答by nos

Yes, C allows you to call your main function (while C++ does not)

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

回答by paxdiablo

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 anecdotalevidence, we should turn to the standard. ISO C11 section 4 Conformancestates:

然而,由于这只是轶事证据,我们应该转向标准。ISO C11 部分4 Conformance指出:

1/ In this International Standard, "shall" is to be interpreted as a requirement on an implementation or on a program; 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. 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.

1/ 在本国际标准中,“应”被解释为对实现或程序的要求;相反,“不应”应解释为禁止。

2/ 如果违反约束或运行时约束之外的“应该”或“不应”要求,则行为未定义。在本国际标准中,未定义的行为用“未定义的行为”一词或省略任何明确的行为定义来表示。这三者的侧重点没有区别;它们都描述了“未定义的行为”。

3/ 在所有其他方面都正确、对正确数据进行操作、包含未指定行为的程序应是正确的程序并按照 5.1.2.3 进行操作。

Now, since there's no explicit prohibitionanywhere 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 termination

1/ If the return type of the mainfunction is a type compatible with int, a return from the initialcall to the mainfunction is equivalent to calling the exitfunction with the value returned by the mainfunction as its argument;

1/ 如果main函数的返回类型是与兼容的类型int,则从最初调用该main函数的返回相当于以该exit函数返回的值main作为参数调用该函数;

And then in section 7.21.3 Files:

然后在部分7.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). If the mainfunction returns to its originalcaller, or if the exitfunction is called, all open files are closed (hence all output streams are flushed) before program termination.

5/ 文件可能随后被相同或另一个程序执行重新打开,并且其内容被回收或修改(如果它可以在其开始时重新定位)。如果main函数返回到其原始调用者,或者如果exit函数被调用,则在程序终止之前关闭所有打开的文件(因此刷新所有输出流)。

Both these sub-sections support the possibility that there may be othercalls to main()over and above the initial/original one.

这两个小节都支持在初始/原始调用之上可能存在其他调用的可能性main()

回答by Tom007

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.

否则,程序永远不会返回并无限运行。

回答by Setu Kumar Basak

Yes you can.

Simple Program:

是的,你可以

简单程序:

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

Explanation:

解释:

A call stackor function stackis 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.

A call stackorfunction stack用于多个相关目的,但使用一个or的主要原因是跟踪每个活动子例程在完成执行时应返回控制的点。

A stack overflowoccurs when too much memory is used on the call stack.

stack overflow当调用堆栈上使用了过多内存时会发生A。

Here function main()is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error.

这里函数main()被重复调用,其返回地址存储在堆栈中。堆栈内存满后。它显示stack overflow error.

回答by Abhishek Attri

Yes, it is possible to call main() inside main() in both C and C++. This is a concept of 'recursion' where a function calls itself.For example:

是的,在 C 和 C++ 中都可以在 main() 中调用 main()。这是一个函数调用自身的“递归”概念。例如:

In C:

在 C 中:

#include<stdio.h>
int main()
{
    static int i=1;  // *
    printf("%d\n",i++);
    if(i==6)
    {
        return 0;
    }
    main(); // recursive call of main() 
}

In C++:

在 C++ 中:

#include<iostream>
using namespace std;
int main()
{
    static int i=1;  // *
    cout<<i<<endl;
    i++;
    if(i==6)
    {
        return 0;
    }
    main(); // recursive call of main() 
}

OUTPUTof both programs:

两个程序的输出

1
2
3
4
5

*here, the statickeyword is used as a basic check to end the program, otherwise it will main() will continue calling itself infinitely. For example

*这里使用static关键字作为结束程序的基本检查,否则main()会继续无限调用自己。例如

1
1
1
1
1
1
1
1
1
1
1.. program ends abnormally.

Here main() calls itself infinitely because new integer 'i' is created everytime in new main() with initial value 1if we don't use statickeyword. So,

这里 main() 无限地调用自己,因为如果我们不使用static关键字,则每次在 new main() 中都会创建新的整数 'i',初始值为1。所以,

if(i==6)

is true in every main() called recursively and the program never ends normally.

在递归调用的每个 main() 中为真,并且程序永远不会正常结束。

But using static keyword creates integer 'i' only once(when main() is executed for the first time) and value of 'i' is incremented during every recursive call of main().So, the recursion ends when 'i' becomes 6 as return 0;executes.

但是使用 static 关键字只创建一次整数“i”(当第一次执行 main() 时),并且在每次递归调用 main() 期间,“i”的值都会增加。因此,当“i”变成时,递归结束6 作为返回 0;执行。

More info. about static keyword: Recursive function with static variable

更多信息。关于静态关键字: 具有静态变量的递归函数

回答by shadab

yes, it is possible to call the main function in itself. It is work as a recursive function but for infinite time(until stack overflow occur).sample code

是的,可以自己调用 main 函数。它作为递归函数工作,但时间无限(直到发生堆栈溢出)。示例代码

回答by REBHU ROY

int static val;
main()
{ 
  if(val==0)
  { clrscr(); }
  while(val<3)
  { val++; 
    printf("calld main\n",main());
  } 
getch();
}