Must program execution start from main, or can the starting address be modified?
#include <stdio.h>
void fun();
#pragma startup fun
int main()
{
printf("in main");
return 0;
}
void fun()
{
printf("in fun");
}
This program prints in fun before in main.
— Richard M. Stallman, The GNU C Preprocessor, version 1.34
Program execution starts at the startup code, or “runtime”. This is usually some assembler routine called
_startor somesuch, located (on Unix machines) in a filecrt0.othat comes with the compiler package. It does the setup required to run a C executable (like, setting upstdin,stdoutandstderr, the vectors used byatexit()… for C++ it also includes initializing of global objects, i.e. running their constructors). Only then does control jump tomain().As the quote at the beginning of my answer expresses so eloquently, what
#pragmadoes is completely up to your compiler. Check its documentation. (I’d guess yourpragma startup– which should be prepended with a#by the way – tells the runtime to callfun()first…)