-2

I want to run a user defined function before the main function in C. But I don't know how to do that. Is that possible?

  • Welcome to SO! Please read [How to ask?](http://stackoverflow.com/help/how-to-ask) (again). Why would you want to do that? You could simply put a function call at the beginning of your main function. What are you trying to accomplish? – Matthias W. Feb 10 '16 at 06:59
  • @MatthiasW.: On bare-metal embedded systems that can be required to initialise memory controller, CPU, etc. But that is implementation specific. – too honest for this site Feb 10 '16 at 14:59

1 Answers1

4

In general, the answer is no.

For an application there is normally no reason to, you simply run any initialization functions, at the top of main() before going into the real application code.

The exceptions usually involve shared libraries. Those can have initialization and termination functions defined that execute after the libc initialization is done but before main() is called, or after main() has returned (or the program is otherwise exited) and before the process terminates.

It is going to be heavily dependent on the OS and libc implementation. On Linux for glibc this link has some info.

Todd Knarr
  • 1,255
  • 1
  • 8
  • 14