Executing a Member Function Before main()

Executing a Member Function Before main()

We can execute any member function or any other execution before main() function by using:

        "#pragma startup".Syntax:      #pragma startup  [priority]//without semicolon


 #includeclass CL{public:	//A constructor	CL(){	cout <> "
The object is creating"; }	//A member function	void obj_func() { cout <> "
This is a function of object"; }};void func_before_main ();	//function declaration#pragma startup func_before_main 65	//setting a function to execute beforemain()void main (){  cout <&;t; "
This is main() ";}// A function definationvoid func_before_main() {	cout << "
This is func_before_main() ";	CL obj1;	//creating an object, and calling its constructor	obj1.obj_func();	//calling a member function of an object}

In defining such functions, the functions should not carry any attributenor return any value. It should be defined before #pragma statement.

Priority is optional. It is ranging from 64 to 100 (recomended).

0-------Highest priority used by C Libraries

63------Lowest priority used by C Libraries

64------First available Priority

100-----Default priority

255-----Lowest priority.

See also  How College Students Can Shape the Future of Tech Responsibility

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist