-1

If we need to call any function we need to call it from main or from the function inside main in nested way. Program starts always from main function.

//fun1.c
fun1(void){
/*Do something useful*/
return 0;
}

//main_fun.c
main(void){
fun1();
return 0;
}

what if we don't want to use main function and want to directly call fun1. Is that possible to directly pointing the program counter to fun1 address so that it will start from there?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Vid
  • 137
  • 1
  • 2
  • 13
  • 6
    Why would you want to do that? – bitcell Nov 10 '14 at 07:03
  • 4
    What are you *really* trying to accomplish? – Keith Thompson Nov 10 '14 at 07:08
  • 3
    Please don't do really strange things, for little or no reason... If you don't want to call your function, then don't. Comment it out. Or don't link main.c. Or use `#ifdef` compiler switches. – Lundin Nov 10 '14 at 07:11
  • seems to be very similar to this SO question http://stackoverflow.com/questions/3974796/in-c-main-function-is-the-entry-point-to-program-how-i-can-change-it-to-an-oth – thurizas Nov 10 '14 at 07:25
  • i don't want to but someone asked me that question in an interview .. so i was curious to know the answer. Sorry for that if u guys think that is a dumb question.. np :D – Vid Nov 10 '14 at 08:29

2 Answers2

2

If you're using GCC or Clang you can use the constructor function attribute.

__attribute__((constructor))
fun1(void){
// ...

It's described in detail here: How exactly does __attribute__((constructor)) work?

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

The C99 and following C11 (see n1570, its latest draft, which is actually the standard in practice) standards define two flavors of C implementations (see C syntax wikipage) .

  • hosted implementations (useful to code application software, e.g. on Linux, POSIX, or probably Windows) give you a standard C library (libc) - giving malloc from <stdlib.h> and printf from <stdio.h> etc etc..., and your program should define a main function (of signature int main(int, char**) or just int main(void)). In a hosted implemention most functions are indirectly called from main. As John Zwinck answered, some compilers give you a way to define functions to be called before main in a very implementation specific way (and order). Notice that the libc usually requires some implementation specific initialization and your main is actually called from crt0 in an implementation specific way.
  • freestanding implementations (useful to code system kernel software, or embedded software on micro-controllers) don't provide a full libc and do not define how the code can be run (and started). In that case your implementation should define how the software is run. In practice you'll need some external (e.g. assembly) code to call functions from your code.

The GCC compiler accepts the -ffreestanding flag to give you a freestanding implementation (otherwise it is hosted)

Notice that hosted implementations are permitted to compile some standard functions in a tricky and magic way (if you #include the standard header defining them). See examples here.

Notice also that the standard is defining how main works in a hosted implementation (§5.1.2.2. Hosted environment page 12 of n1570). In particular main is the only function where the lack of return is the same as return 0; (also the standard exit(3) function would end the program nearly as if returning from main).

In practice your question is implementation specific.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547