Call main() itself in c++?

#include <iostream>
#include <cstdlib>

int main() {
    cout << "!!!Hello World!!!" << endl;
    system("pause");
    return main();
}

The above works, but it hardcoded the main() function. Is there a magic variable or macro to get the current running function?


Is it allowed in "C++"? No.

In practice, can you call main()? Yes.

Whatever the C++ Standard says, that doesn't stop the Linux g++ compiler from compiling code with main() in main().

#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
 int y = rand() % 10; // returns 3, then 6, then 7
 cout << "y = " << y << endl;
 return (y == 7) ? 0 : main();
}

Which lets us do:

 > g++ g.cpp; ./a.out
 y = 3
 y = 6
 y = 7

Looking in to the assembly, we see that main is called just like any other function would be:

main:
        ...
        cmpl    $7, -12(%rbp)
        je      .L7
        call    main
        ...
.L7:
        ...
        leave
        ret

Not that this behavior is guaranteed, but it looks like g++ doesn't seem to really care about the standard, apart from this sarcastic warning with -pedantic

g.cpp:8: error: ISO C++ forbids taking address of function '::main'

Can main function call itself in C++?, The main() function can call itself in C++. This is an example of recursion as that means a function calling itself. A program that demonstrates� Well, from the birth of C people are not calling main inside main and will probably never. What you do is a recursion which is not a very efficient way to implement algorithms, as it may lead to lockups, unnecessary use of extensive memory as a function continuosly calls itself.


The C++ Standard says that you may not call main() from your own code. As for getting the name of the current function, you could use the __FUNCTION__ macro, but once again this is not standard:

#include <iostream>
using namespace std;

void foo() {
   cout << __FUNCTION__ << endl;
}

int main() {
   foo();
}

should print "foo" or something similar if __FUNCTION__ is supported.

How to call the main function in C program?, Although you can call the main() function within itself and it is called recursion. Recursion is nothing but calling the same function by the� Possible duplicate of Can main function call itself in C++? – JL2210 Mar 25 at 20:19 | show 3 more comments. 4 Answers active oldest votes. 12. The C++ Standard


If a specific implementation allows this, it is not behaving correctly(a). The standard state quite explicitly in C++14, 3.6.1 Main function /3:

The function main shall not be used within a program.


(a) Keep in mind that many implementations follow some parts of the standard loosely, preferring power over strictness. That can have the unfortunate side effect that your code may not be portable to other compilers or even other versions of the same compiler.

Many implementations will also allow you to take the stricter view, such as using g++ -std=c++11 -Werror=pedantic which catches the particular issue bought up in this question, as well as quite a few others. It is that "mode" of translation that allows implementations to claim to be compliant with the standard, as per 1.4 Implementation compliance:

If a program contains a violation of any diagnosable rule ..., a conforming implementation shall issue at least one diagnostic message.

You'll see it's still quite possible to allow the code to compile and run in that case, since "diagnostic message" can mean a warning rather than an error.

Can I call the main () function within itself, I wanted to know if i could call the main () function within itself as shown, #include <stdio.h> main () { int a,b,c; char ans; printf ("Enter the first� In the above example sum(a,b); is function call . a,b are parameters passed to function ‘sum’ Function call should be made by ending Semicolon ; Dissecting a C Program Calling a Function : Call a C function just by writing function name with opening and closing round brackets followed with semicolon.


Generally, no. For now it will be enough for you to know that the compiler needs to know the exact function you're calling at the compile time. You cannot do magic like, let's say

func = "my_function"; 
func(); 

if the called function name will change during runtime. (There are exceptions and ways around that, but you don't need that).

Don't think about that as a case of hard-coding: it is not. If you need to call the function, then you just write its name, and don't try to abstract it, or something.

Also, now would be a nice way to learn about the while loop, infinite loops and write without the function calls at all, e.g

int main()
{
    while (1) {
        cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
        system("pause");
    }
}

Can main() be called recursively?, In C/C++ the [code]main[/code] function is just like any other function. Just like other functions, you can call main from anywhere you want. as a basic check to end the program, otherwise it will main() will continue calling itself infinitely. Possible duplicate of Can main function call itself in C++? – S.S. Anne Mar 25 '19 at 20:19 The C++ Standard says that you may not call main() from your own code.


C/C++ program for calling main() in main(), C program to illustrate calling. // main() function in main() itself. #include "stdio.h". // Driver Code. int main(). {. // Declare a static variable. static int N = 10;. It is the main entry point of program that executes all the objects and invokes method to execute. There can be only one Main method in C#. However, the C# Main method can be void or int return type. It must be inside a class or struct and must be declared with static modifier. It is the main place where a program starts the execution and end.


Recursive call to main() in C, void A() { A(); return; }. A() is a recursive function since it directly calls itself. C <- ---|. This can be viewed in the following three functions: void C() { A(); int main() { int Result, num;. cin >> num;. cout << "Result: " << Recursive(num) << endl;. void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); } The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.


Recursive Functions, A function that calls itself is called a recursive function. In the beginning main() function called rec() , then inside rec() function, it called itself� Back in the old days, programs featured command-line options or switches. For example, to compile and link a C program, you would type something like this: cc ex1501.c -o ex1501 The three tidbits of text after the cc command are options or switches. They are also arguments to the main() function. A program can read …