aspose file tools*
The moose likes C / C++ and the fly likes main() function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "main() function" Watch "main() function" New topic
Author

main() function

Nakul P. Patel
Greenhorn

Joined: May 31, 2011
Posts: 25
1>Is main() function a User Define Function or Pre Defined Function?
with reason.

2>Can i run a c/c++ program without using main() function?
with reason(code).
Anthony Aj Williams
author
Ranch Hand

Joined: Jun 10, 2011
Posts: 56
Nakuli Patidar wrote:1>Is main() function a User Define Function or Pre Defined Function?
with reason.


main() is a user-defined function, which must have a particular signature, and must not be called directly from within a C++ program (though it may be called from within a C program).

Nakuli Patidar wrote:
2>Can i run a c/c++ program without using main() function?
with reason(code).


Every C and C++ program must have a main() function, unless it is running on a "freestanding implementation", (a term used to imply a limited implementation, probably on an embedded system without an underlying OS). However, that main() function may be provided by a library, so the user-provided entry point is different.

Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++11 thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130


must not be called directly from within a C++ program (though it may be called from within a C program).



can any one please explain this with example ?
Anthony Aj Williams
author
Ranch Hand

Joined: Jun 10, 2011
Posts: 56
abhay jain wrote:

must not be called directly from within a C++ program (though it may be called from within a C program).


can any one please explain this with example ?


In C, you can write:



This recursively calls main with decreasing values of argc until it reaches 0. This is valid C, and is the basis for many of the Obfuscated C entries.

On the other hand, this is not valid C++ (even though it is syntactically valid), because the C++ standard forbids it.
Brian Overland
author
Ranch Hand

Joined: Sep 09, 2011
Posts: 36
AJ's knowledge of the C++ spec is superb, and I bow to his knowledge. I am being sincere, not sarcastic. In all my decades of C and C++ programming, I never tried calling main() from within main() -- but I find AJ's comment fascinating.

I have one thing to add: Generally, you are going to need a main() in an freestanding program (such as an EXE file in Windows or old MS-DOS)... otherwise, there is no entry point.

However, it strikes me that certain kinds of executables -- specifically DLLs or Windows programs -- may not have a main() function. That is because their entry points are through other means. A Windows program, for example, is in some ways more like a DLL (dynamic-link library) than a freestanding execution. Windows will call the program through its own special entry point that it recognizes (WinMain, not predefined in C or C++ itself).

AJ -- correct me on this if I'm not mistaken.

But all that really matters is the following: you obviously need a main() function in a freestanding program (as opposed to a program called by Windows or through a DLL) because otherwise C++ has no idea what to use as the entry point... that is, where to start executing.

An interesting contrast is assembly language... which is very close (has a 1-to-1 relationship with) machine language. The first assembly-language or machine instruction is where the program starts: just start executing the first line!

But higher level languages, including C++, C#, Java, Basic, LISP, etc., do not work that way. The reason is that in a higher level language, there is a certain amount of overhead... even for C and C++... that must be set up so that the rest of the program can correctly execute. Stack area and heap area must be set aside, certain variables associated with the run-time library must be initialized, and other stuff must be set up that normally you don't have to think about.

Furthermore, the syntax of higher-level languages usually would make things too sloppy to just have executable statements standing outside of function or procedure definitions... although very old versions of Basic (BASICA) for many years did just that!!

So where does a program begin executing? Another solution, I suppose, would be for the program to just start executing with the first function that appears in the program -- the one nearest to line 1.

But there is an exellent reason C++ (and other languages) do not do that. The reason is: some programs may have multiple modules... meaning that the program consists of code from multiple source files, compiled and then linked together.

In that case, which function in which module should begin executing first? Again, C++ would have no way to know. The easiest solution by far is just to use the reserved word "main" to always mean the entry point for a stand-alone executable program. And so that's what C and C++ do.

== Brian Overland
Anthony Aj Williams
author
Ranch Hand

Joined: Jun 10, 2011
Posts: 56
Brian Overland wrote:Generally, you are going to need a main() in an freestanding program (such as an EXE file in Windows or old MS-DOS)... otherwise, there is no entry point.


I think you are using "freestanding" here in a different sense to that used in the standard. A "freestanding implementation" is essentially one without an OS, and with a limited standard library. The standard specifies fewer constraints on freestanding implementations. Most compilers that target "normal" operating systems like Windows and MacOS are "hosted implementations", which provide a full standard library.

Brian Overland wrote:However, it strikes me that certain kinds of executables -- specifically DLLs or Windows programs -- may not have a main() function. That is because their entry points are through other means. A Windows program, for example, is in some ways more like a DLL (dynamic-link library) than a freestanding execution. Windows will call the program through its own special entry point that it recognizes (WinMain, not predefined in C or C++ itself).


DLLs are not covered by the C or C++ standards, and as such platforms can provide their own rules for them.

On Windows, you can write a main function if you like, but the compiler will probably build you a console app rather than a GUI app. The entry point called by the OS is actually something else entirely, and calls the constructors for global variables, and initializes the runtime library before calling the user-provided entry point.

The use of WinMain is a non-standard extension, but then if you're writing a Windows GUI you're already using non-portable facilities.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main() function