C program without main()

India
February 21, 2007 11:55pm CST
Can we write a C program without a main() ??
6 responses
• India
3 Mar 07
It is not possible. But in the following conditions it may work. 1. While writing the library. In which the main() will be present in some other file. 2. Using the #define for main() 3. If you want to call some function before main() initialization then we can use class A { A() { printf("Hi"); } } A temp; void main() { printf("ha ha ha"); }
1 person likes this
• India
3 Mar 07
I doubt whether 3rd one works or not but still good..
@dholey (1383)
• India
23 Feb 07
in the manual of c it is specifically mentioned that "ONE C PROGRAM MUST HAVE ONE AND ONLY ONE main() FUNCTION" SO THERE IS NO ALTERNATIVE .... ALL THE C PROGRAM MUST HAVE A MAIN FUNCTION WEATHER IT IS DIRECTLY WRITTEN OR WRITTEN THROUGH # define ... or other methods .... but one main must be there ... there is no alternative for it ....
1 person likes this
• India
26 Feb 07
Correct.. But here I was expexting the #define method which indirectly solves it.
• India
23 Feb 07
Hi.. This is another one of the way.. #define A main(){ A printf("Nice Example isn't."); } if u satisfied ..don't forget to rate me my friend...
1 person likes this
• India
23 Feb 07
Good one .. Rated plus
• India
3 Mar 07
I pesonally feel that the c program without main is impossible.. Even though the #define method looks as if working but we should not forget that it is a preprocessor directive and before compiling it will be replaced by main()
1 person likes this
• India
22 Feb 07
I think the main function should be under main().but you can include other c file in a main function.then the other c file will not have main().
• India
23 Feb 07
I think it is one of the way.. Other ways must be there..
@ebo_bro (190)
• Philippines
23 Feb 07
the compiler will always look for the main function first, you don't have to put it on top. you can place it in the middle or at the end of your program. The important thing is that you have a main function. well that what my teacher told us before. And I think its true, once I accidentally misspelled Main() and it didn't complied