Tuesday, December 11, 2007

C/C++ Tips

Q: How to call a function without calling inside main() function in C/C++ ?

A:
#pragma startup [priority]
#pragma exit [priority]
These two pragmas allow the program to specify function(s) that should becalled either:
■ upon program startup--before main() is called,
or
■ upon program exit--just before the program terminates through _exit
■Function must be declared or defined before the #pragma line reached.

Example:
#include
void f(void);
#pragma startup f
void main()

{}
void f()
{
printf("i'm called before main()...");
}

Happy Programming!

No comments: