Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
where is the main() function located ? can we see its definition in the compiler files like header files?
Posted
Updated 16-Jul-12 4:30am
v2
Comments
[no name] 16-Jul-12 10:39am    
http://www.codeproject.com/Questions/331914/is-the-main-in-c-is-user-defined-or-predefined-fun

Go to Bjarne Stroustrup's homepage[^] and spend some time learning the basics of the language. It will be faster (and better) than posting individual questions here.
 
Share this answer
 
Comments
[no name] 16-Jul-12 22:52pm    
First step - Google 101.
You main function WILL be somewhere within your compiled files. I'm going to go out on a limb and guess that you're using some sort of framework (MFC, Qt, etc.) that's hiding the actual main() function entry point into your program. If that's what you're asking, yes, it's somewhere in the files that are included with the framework and if you can't see it, it's usually because you shouldn't have to override it in most cases. The reason you don't want to override their implementation... because they do specific things within the main that make the framework work correctly, so don't change it unless you really know what you're doing.
 
Share this answer
 
Any of these prototypes are good to use:
int main(int,char[]*)
int main()
void main()
It is no need to declare it. In the startup executable code of the program is a call to this function. You only have to implement it.
 
Share this answer
 
Comments
Stefan_Lang 16-Jul-12 10:38am    
I disagree on void main(). It may work, but it's not good. ;-)
armagedescu 16-Jul-12 12:07pm    
I only agree that it is not standard and have to be avoided. But if you use it, not a big deal.
I don't agree that "it may work". It works. Not for me. It just works :) In some discussions in the internet is said that it might crash because copy/paste "could lead to stack corruption in the program's exit sequence", and is followed by a code which is meant to demonstrate how it can corrupt the stack :). But exactly that copy/paste code from disassembler proves that it has absolutely no impact on the stack :D. Return value of C integer has no impact on the stack, no matter how many times you return. The return value is returned to the system as exit code. So, if you don't care about exit code, then no problem. Even in void main you can return needed exit code with void exit(int) function.
Emilio Garavaglia 16-Jul-12 14:45pm    
The point is not if it "works" or not. i that is it "standard" or not. And not: it is not standard since the language specification don't specify any void main().

Actually it is just a microsoft extension other implementors support, but that is not granted to be supported everywhere because no one is formally required to implement that capability.

armagedescu 16-Jul-12 15:40pm    
Oh noo, holly wars again. No one intended to add any exception. And in Microsoft documentation is no reference to void main. It is mostly a side effect of how C language works. Function main is C function. And in C you are not obligated even to declare the type of returned value. But if you declare some return type, you are not obligated to return anything. This is why it works, and just no one cares about void main, excepting holly wars.
Emilio Garavaglia 17-Jul-12 2:29am    
... "And in Microsoft documentation is no reference to void main".
It no "holy war": it's history. void main() was introduced by microsoft C in 1988. Subsequent standardization of both C (from 1990) and C++ (1996/99/03/11) never formalized that variant.

Today MS adhere to the standard and don't mention the fact that void main exist and compiles mainly to retain backward compatibility.

As per the standard, a C function with no return type specifyed is int not void.

void main is an extension, not a standard part of the language. And being an extension, compiler writers are not required to implement it.

If you are convinced about this is just a war, please cite me the page and line of the ISO specification of the C and C++ languages of 2011 that state void main is part of the standard.

I can agree with you this is probably not the most important thing in programming, but the C language is what the ISO cometee says it is. Your and my opinoins about it are irrelevant in that context.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900