-2

Possible Duplicate:
C program without main function ?

Can we write a c program without main() which can run and execute?

Please reply...

Community
  • 1
  • 1
  • 2
    why are people curious about this at all? what possible value could an answer other than "no it's not possible" have? or is it meant to be a purely philosophical thought exercise? No offense intended, I am just really surprised this question has come up more than once. – tenfour Oct 05 '10 at 13:48
  • It is possible, but it is extremely specific to the OS, some standard library stuff (if you care to use things like `FILE *stdin`, and several other things), and may even be processor specific. Many other compiled programming languages use C's pre-main code, and so have `main` but not all do. They are not linked against one of the crt*.o (or similar), but need their own startup code. – nategoose Oct 05 '10 at 14:34

2 Answers2

0

No, you can not. You can write a library that will be used from another program. But everything must have a beginning, and 'main' is the beginning of a C program...

(On Windows, the Win32 API specifies a WinMain, but it's the same thing, with a different name.)

florin
  • 13,986
  • 6
  • 46
  • 47
  • 3
    That only holds true for hosted implementations; a freestanding implementation can use an entry point other than `main`. See 5.1.2.1, paragraph 1. – John Bode Oct 05 '10 at 13:47
0

No, without the main method, the linker will not know where the start of the data segment in the program will start.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228