简体   繁体   中英

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:

#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:

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:

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.

Now, since there's no explicit prohibition anywhere in the standard on main() calling itself, clause 3 above is the controlling aspect.

Further support can be seen in two places (my bold), first in section 5.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;

And then in section 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 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.

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

Yes, we can call the main() within the main() function.

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.

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.

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

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 .

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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