Is it possible to call main()
within the main()
function in c?
-
15Did you try it? Did it work? – Donnie Nov 21 '10 at 13:53
-
1Nothing prevents you from calling a function within its context. It's called recursion. – bcosca Nov 21 '10 at 13:54
-
31@Donnie just because something would appear to work in a test example doesn't mean it always will, or is correct. "try it and see if it works" is often dangerous in C. – nos Nov 21 '10 at 13:57
-
6@nos: I still would have liked to see the author cared to at least try first and share his/her experiences before asking the question. I don't fancy people using this as an interactive Google interface. – Sedat Kapanoglu Nov 21 '10 at 13:59
-
7It's a legitimate question, e.g. the C++ standard specifically disallows it, while a "try and see if it works" approach would likely shown that "it works" if this question was about C++, yet it would not be correct. – nos Nov 21 '10 at 14:01
-
1"Did you try it?" has little to do with the question. There are plenty of things ridiculous compiler implementations allow (like `void *x; x+=42;`) which are not legal C. This, however (recursive calling of `main`) is legal C. – R.. GitHub STOP HELPING ICE Nov 21 '10 at 14:01
-
@nos: Unless your version of C++ happens to call global constructors from an invisible first-statement in `main`, and then ends up calling them every time you recurse into `main`... ;-) – R.. GitHub STOP HELPING ICE Nov 21 '10 at 14:02
-
2If you want examples of `main()` calling `main()`, here are plenty: http://www.ioccc.org/ It's the website of the International Obfuscated C Code Contest, and folding everything into a single function called `main` is an effective obfuscation technique. – cmaster - reinstate monica Dec 08 '15 at 07:03
-
1@cmaster many of the IOCCC entries don't comply with the ISO C standard – M.M Dec 08 '15 at 07:14
-
@M.M That's true. But they do make for *interesting* examples ;-) – cmaster - reinstate monica Dec 08 '15 at 22:01
-
This can also be used for code-golfing. – Weijun Zhou Mar 19 '20 at 16:01
-
My winning entry in the 17th IOCCC recursively called main(), a program which graphed polynomials. Mine most certainly did comply with the C99 standard: https://www.ioccc.org/years.html#2004_hoyle – Jonathan Hoyle Aug 05 '22 at 21:50
4 Answers
Yes, C allows you to call your main function (while C++ does not )
-
3
-
17@ThiefMaster: The simplest implementation of global constructors (without special support from the OS and underlying C runtime entry code) is for the C++ compiler to generate a function call at the beginning of `main` (`__main` is a common name for it) which calls all the global constructors. Having global objects be reconstructed every time `main` gets called recursively would be a rather bad thing... :-) – R.. GitHub STOP HELPING ICE Nov 21 '10 at 14:04
-
1@nos Could you point to a place in any C standard that specifically addresses `main()` recursion, if it is specifically addressed? – recursion.ninja Aug 17 '13 at 15:52
-
I think you should link [this](https://stackoverflow.com/questions/2128321/can-main-function-call-itself-in-c) instead. – user202729 Aug 16 '18 at 11:08
-
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 withint
, a return from the initial call to themain
function is equivalent to calling theexit
function with the value returned by themain
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 theexit
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.

- 99
- 1
- 6
-
1It will cause a stack overflow when the stack space is used up. – Jens Mühlenhoff Sep 14 '14 at 17:52
-
2Except when the compiler does tail call optimization, but anyway you can't rely on that so endless recursion is to be avoided. – Jens Mühlenhoff Sep 14 '14 at 17:56
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
.

- 11,460
- 9
- 53
- 85