0

I was going through the difference in C and C++ and I found a tricky point. Can you please elaborate the below points:

  1. In C, we can call main() Function through other Functions.
  2. In C++, we cannot call main() Function through other functions.

How to call main() from another function and what is the use case of it?

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
Akankshi Mishra
  • 65
  • 2
  • 10
  • Possible duplicate of [Call main() itself in c++?](http://stackoverflow.com/questions/2532912/call-main-itself-in-c) – mash Jul 25 '16 at 06:32
  • 4
    You can, but it isn't good practice. If you ever feel like you'd want to, then create another function with the same signature and call it instead. – Klas Lindbäck Jul 25 '16 at 06:41
  • how? just write `main()` or `main(argc, argv)`. There's no difference from other functions – phuclv Jul 25 '16 at 06:42
  • 3
    There is no real world use case for calling `main` from another function AFAIK. – Jabberwocky Jul 25 '16 at 06:57
  • The use cases, IMHO, are: 1. a very bad way to run a loop while bombarding the stack in an attempt to bring a machine to it's knees... 2. terrible recursive argument parsing... There's a reason C++ thought it should protect itself from people who can't write a `while` loop... – Myst Jul 25 '16 at 07:08
  • The macOS java executable (yes, *that* executable) calls `JLI_Launch` which eventually re-executes the java `main`. It is not, I repeat *not*, pretty. – Jeff Holt Jun 21 '21 at 20:49

1 Answers1

1

@TrevorHickey hit the nail on the head (where did his answer go?) - C++ forbids calling main from within a different function (for good reason)... Not that any compiler is likely to stop you (I don't think most of them care).

An obvious workaround would be to move main's functionality into a container function and call it from there, as suggested by @KlasLindbäck.

i.e.

int my_application(int argc, char const * argv[]) {
   // do stuff
   return 0;
}

int main(int argc, char const * argv[]) {
   return my_application(argc, argv);
}

Another "hack" that probably only works because compilers let you call main anyway (As also pointed out by @KlasLindbäck in the comments), would be to use function pointers. i.e.

int main(int argc, char const * argv[]) {
   // do stuff
}

// shouldn't compile... but hey, you never know.
int (*prt_to_main)(int, char const* argv[]) = main;

void test_run(void) {
   prt_to_main(0, NULL);
}
Myst
  • 18,516
  • 2
  • 45
  • 67
  • I think any compiler that is lenient enough to allow to take the address of `main` into a function pointer would also be lenient enough to allow you to call it directly. GCC is one such example. – Klitos Kyriacou Jul 25 '16 at 07:05
  • @KlitosKyriacou, I totally agree. I don't think most of them do more then issue a warning anyway... not that I've tried (I can see no good coming from calling `main` in any way). – Myst Jul 25 '16 at 07:10
  • 1
    You cannot call `my_application()` with no arguments. – Aykhan Hagverdili Jan 20 '19 at 11:54