Question:
Can we write a C program without main()?
annie
2007-02-24 09:56:41 UTC
Can we write a C program without main()?
Five answers:
csanon
2007-02-24 15:53:15 UTC
Some incorrect answers here. First, to counter what others have said:



@Sandeep:

Try writing a windows app. Why is there no main?



@Apoorlone:

Include files don't get compiled. Libraries are compiled already.



@Balabek:

"Operating systems don't always call this function, it actually depends on platform, hence there is not always need for you to implement it."

Except C and C++ are portable...



"All Windows programs have that [WinMain()]"

Nope. Only the ones that write a Windows GUI program. You can write a console app without WinMain.



"But when writing a console program for Windows, static linking libraries that implement WinMain() will be provided"

Wrong again. What libraries are these? Are you talking about the C and C++ runtime libraries? Take a look at the compiler code for gcc, or read up on implementation. You'll see that WinMain() is *not* called for a console program.



"Try writing a console program without a main() call, and you'll see a linker error that says function definition is not defined."

Yeah because mainCRTSTartup calls main(), but main() doesn't exist. Try dropping standard libs with compiler flags, and you'll see it complain about mainCRTStartup instead of main().



"all initalization before calling main() and all cleanup after main() function returns are handled automatically."

Unless you take over that part. In which case you don't need main.



Yes, you can write a C program without main().

- You aren't writing a console program. Dlls, static libraries, Windows programs qualify.

- You redefined the entry point through compiler options.

- You dropped standard compiler libraries, so now you have to implement mainCRTStartup yourself.
Francine
2016-03-29 02:42:39 UTC
Absolutely you can write a program without a main. The 'main' entry point is only used as the starting point for 8-bit character set command line applications. If you create a default command line application in Visual C++ it will offer up a character set agnostic entry point of _tmain, so the application can receive Unicode parameters if compiled that way. If you create a Windows application you will be given WinMain as your entry point. Staying with Visual C++, if you look at the properties for a C++ project, on the Configuration Properties for the Linker in the Advanced item you'll find an item for setting the entry point. Being able to set the entry point is common to many languages. Java has an analogous setting where you can specify the start. Most C++ compilers or linkers will have a similar setting. It is worth noting that main is rarely the actual entry point of an application, rather it is the place where the run time libraries for your compiler hand over to your user code. The operating system will use a different entry point which will set up the runtime environment before the hand over.
mirbeksm
2007-02-24 10:16:37 UTC
it depends.



int main(int argc, char argv**);

is the most common (I would say standard) function declaration for main function.

Before a program is executed, it gets loaded into memory, standard I/O buffers are initialized, etc...

Operating systems don't always call this function, it actually depends on platform, hence there is not always need for you to implement it.



For example, in Windows platforms, instead of main() you'll have WinMain. All Windows programs have that.

But when writing a console program for Windows, static linking libraries that implement WinMain() will be provided, and within that implementation there will be a call to your main() function.



Try writing a console program without a main() call, and you'll see a linker error that says function definition is not defined. It is because the WinMain implementation is calling your main() function (in static linking), but you haven't provided any.



The platform specific details are hidden, all initalization before calling main() and all cleanup after main() function returns are handled automatically. So you can treat your main() function as the main body of your program.



It is generally better to conform to this 'standard'. Otherwise you'll go through a lot of trouble. For example, when you run a console program a screen is provided that allows you to scroll up/down, accept buffered text input/output, handles mouse inputs/ etc. All these functionalities are provided in some code that is provided in some library, it is quite a convenienve that such standard program entry point exists..
Blistika Barnacles
2007-02-24 10:01:38 UTC
no one cannot write a c program without main under normal circumstances

main is the basic function from which the execution starts
apoorlonesomegeek
2007-02-24 10:07:54 UTC
yes for includes files or for a libraries


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...