Call Main() Itself in C++

Can main function call itself in C++?

TLDR: Calling main results in undefined behavior.


There seems to be confusion about the terminology used in the standard, and the implications that has for the programmer and compiler.

Firstly, the standard alone determines everything about the C++ language. If your particular version of a particular compiler allows some particular action, that has no bearing on whether or not that action is legal. For the remainder of the post, I'm referring to the ISO03 standard.

So to quote once again, the standard says in §3.6.1.3:

The function main shall not be used within a program.

Additionally, §3.2 defines "used" as:

An object or non-overloaded function is used if its name appears in a potentially-evaluated expression.

This means that once the program begins executing, main should never be entered again. That means programmers cannot call main, that means the compiler cannot insert another call to main (why it would, who knows), you cannot take the address of main and call that, etc. You cannot even have the potential of calling main.

The only call to main should be by the run-time library the program is running on; all other calls invoke undefined behavior. (Which means anything could happen!)


Now onto compiler behavior:

A diagnosable rule is defined as (§1.4.1):

The set of diagnosable rules consists of all syntactic and semantic rules in this International Standard except for those rules containing an explicit notation that “no diagnostic is required” or which are described as resulting in “undefined behavior.”

In our case, §3.6.1.3 defines a diagnosable rule. Here's what compilers should do according to §1.4.2:

— If a program contains no violations of the rules in this International Standard, a conforming implementation shall, within its resource limits, accept and correctly execute3) that program.

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

— If a program contains a violation of a rule for which no diagnostic is required, this International Standard places no requirement on implementations with respect to that program.

So compilers are not required to enforce rules. All compilers have to do is take well-formed programs (§1.3.14) and turn them into an executable program. A compiler is free to warn, error, etc. however it likes, as long as it does not conflict with the language. It is required to display a message in our particular case, according to the second clause.

For this particular problem, on gcc the -pedantic option will warn about the illegality of calling main within the program. Visual Studio will not warn about calling main, but on any warning level (greater than 0) it will warn about the recursive nature of the program.


What does all this mean in terms of the answers you should expect? It means it's completely meaningless to try and define with certainty what the code snippet posted will do. Calling main results in undefined behavior, and trying to define undefined behavior is obviously a lost cause. The only honest answer anyone can give to "what happens when I call main?" is "Anything."

I hope this clears things up.

main() function calls itself in C++, what will happen?

That's undefined behaviour. You cannot call main() from within a C++ program (section 3.6.1.3 of the standard).

Therefore anything can happen. And there's no point in asking why.

Why calling main() is not allowed in C++

In addition to the other answers: The c++ spec guarantees that all static initialization has happened before main is called.

If code could call main then some static scoped object could call main, in which case a fundamental guarantee is violated.

The spec can't say "static scoped objects should not call main()" because many objects are not written specifically to be instantiated at static scope always. It also can't say that constructors should not call main() - because its very difficult to audit and prove that a constructor isn't calling a method, calling a method, that might sometimes, call main().

why main function run first in c/c++?

Because that's what the Standard defines the language to use (C++ quoted here):

[basic.start.main]

A program shall contain a global function called main. Executing a program starts a main thread of execution (...) in which the main function is invoked (...)

So the compiler has to produce the binary in a way that calls main when the program is started by the operating system, or, in case of freestanding environments, when it's loaded.

Technically speaking, it doesn't have to be the first call in the resulting assembly. The compiler can insert some additional startup code (like initializing variables etc.), which can itself be grouped into functions. This is out of concern of a C++ program developer, but becomes quite important on embedded systems, where you need/want to be aware of almost every instruction executed.

In C, calling a function from main

you defined it again..
Just remove the void from the funciton.
include a header a forward declartion so it will recognize it..

void printSum (void); <-------------------

int main(void){

printSum(); <-------------------------
printf("Hi!\n");

return 0;
}

void printSum (void)
{
printf("Please give two integers\n");
int x,y;
scanf("%d %d", &x,&y);
printf("%d + %d is %d\n",x,y,x+y);
}


Related Topics



Leave a reply



Submit