Re: execute a different function before main()



aarklon@xxxxxxxxx wrote:

On Mar 12, 7:28 am, George Peter Staplin
<georgepsSPAMME...@xxxxxxxxxxxx> wrote:
aark...@xxxxxxxxx wrote:
On Mar 11, 9:34 pm, Eric Sosman <Eric.Sos...@xxxxxxx> wrote:
aark...@xxxxxxxxx wrote:
Hi all,
recently i read the following passage in a book

it is a compiler dependent thing if some feature is available
that allows you to execute some function before main() is
called. In turbo C compiler, there's one such fearture....the
#pragma startup directive.
I m giving an example of its use:-

#include<stdio.h>
void fun();
#pragma startup fun
int main()
{
printf("Arun");
}
void fun()
{
printf("Anand \n");
}

The o/p of the program given above will be :- Anand

my question is there a standard way in ANSI-C
to execute a different function before main() is
called....??????

Yes. Two ways, in fact:

1) Run your program in a freestanding environment, where
"the name and type of the function called at program startup
are implementation-defined."

is it possible to modify the start up code in a hosted environment
so as to change the name and type of the function called at
program
startup..???

Yes, it's possible with a hosted environment but not from standard C.

When a program is loaded with most (Windows and Unix-like) systems,
the OS jumps to an offset for _start in assembly code.

If you're using gcc in Windows or a POSIX system you can see what
startup files gcc links with via: gcc -v somefile.c

The startup files are typically named like crt0.o. They are
implemented in assembly code usually and the various crt*.o do tasks
like implementing exit(main(argc,argv)); and setting up the
environment,

Can any one the general outline of the life cycle of the execution a
C program, in similar lines to the "LInux boot sequence"
given here
http://www.debianhelp.co.uk/boot.htm

No, because different implementations will do this differently. Even for
Linux the start-up sequence is very different under x86 and Itanium.
For hosted implementations the system will typically jump into a
predefined entry point in your program. This normally enters the
initialisation code of your C runtime library that will linked in with
your program. Often the entry point is specified in source with the
label _start:. This code does some system specific initialisation and
ten calls the function called main, which of course, then enter your
program. After you return from main, fall of it's end, or call exit,
_Exit or abort, execution once again enters the shutdown code in your C
library, which may do a whole host of things like flushing streams,
closing file handles, calling handlers registered with atexit etc.,
before finally handing control over to the host system.

To know the details you need to look at the source for your system C
runtime library. The details are very system specific and no one
explanation will suffice.

.



Relevant Pages

  • Re: execute a different function before main()
    ... allows you to execute some function before mainis called. ... there's one such fearture....the #pragma startup ... it's possible with a hosted environment but not from standard C. ... If you're using gcc in Windows or a POSIX system you can see what ...
    (comp.lang.c)
  • Re: Getting STARTUP to run
    ... Perhaps you didn't keep the calc OFF ... Normally, turning ON does not execute STARTUP, ... I also put that inside an endless IFERR loop which simply ...
    (comp.sys.hp48)
  • Re: installing resident images - problem
    ... >>>>>I am getting errors trying to install resident images at startup. ... startup jobs that get submitted to batch that might actually execute after ... Should I not just then be able to increase GH_RES_CODE by that amount, ...
    (comp.os.vms)
  • Re: execute a different function before main()
    ... allows you to execute some function before mainis called. ... there's one such fearture....the #pragma startup ... If you're using gcc in Windows or a POSIX system you can see what ... The startup files are typically named like crt0.o. ...
    (comp.lang.c)
  • Re: Getting STARTUP to run
    ... The problem is that it doesn't execute on powerup. ... Normally, turning ON does not execute STARTUP, ... if you store your display program in STARTOFF ... I also put that inside an endless IFERR loop which simply ...
    (comp.sys.hp48)