Welcome, %1$s. Please login or register.

December 12, 2017, 12:05:45 PM
: 1
: User function before main()  ( 4569 )
« : May 19, 2007, 11:45:41 AM From Ria»



User function before main()


Is it possible to call ones function before the main get called???

 
Liked It? Share it!

              


« #1 : May 21, 2007, 12:28:37 AM From Sarbartha Sengupta»

main is the function from where the execution is started. so no function will not being executed before main.
u can write any function before main but the compilation will start from main. if you call that function from man or any other function which is called or oher way linked with the main then that functio an be executed.   
« #2 : May 21, 2007, 07:11:21 PM From Deepak»

It is possible !!!
Generally we write the program for two purposes..
1. that runs under any os say windows, linux
2. that runs in a embedded system i.e., mobile or any electronic device.

so in 2nd type of programs we can call anyother function (even we can escape main()!!!)
« #3 : May 21, 2007, 08:59:48 PM From Prateek»

Even in application programs that run on traditional operating system it is possible to invoke a user-defined function before main.

All the global variables get initialised first before the main() is invoked. Also, similar to local variables, global variables also can be initialised by calling a function.
eg.

int generateRandomNumber(void);

int globalVariable = generateRandomNumber();

main()
{
    return 0;
}

int generateRandomNumber()
{
    return rand();
}


This peice of code will invoke generateRandomNumber() before calling main()
« #4 : June 18, 2007, 07:46:26 PM From eashwar»

we can use #pragma starup
« #5 : June 18, 2007, 09:11:55 PM From Poonam»

Eashwar, can you give an example of how to use that pragma?
« #6 : June 19, 2007, 12:37:35 PM From Ravikumar.S»

I don't agree by prateek's way of using function assignment for a global variable. This will give compiler error.

Other way is to set the "EntrypointSymbol"  in the Link options to whichever function is needed to execute. In this case main is not at all used.

Example: In case of Microsoft Visual Studio we can specify the same in the Link Output options.
Even some of the embedded IDE's like ADS, Codecomposer studio allow a similar way to set the entrypoint function.
« #7 : July 29, 2007, 02:01:06 PM From pj»

what sarbartha told is a bit wrong.compilation doesnt start from main function.it starts from the first line of the program but execution starts from the main function.
« #8 : January 10, 2008, 01:54:42 PM From thequark»

If you want to follow ANSI standard then I guess you don't have a choice, this can't be done.

Otherwise each compiler will have its own tweak to achieve this. Gcc has a way to tap in function calls. If a program is compiled with a specific flag [which i hv forgot] the programmer has to declare a certain function of certain fixed prototype [which also i have forgot] now this function gets calleded _before any_ function is called during execution. I have not checked but I guess this should include main also.

int globalVariable = generateRandomNumber();

gives error while using gcc [in Windows]
« #9 : January 10, 2008, 06:47:00 PM From iammilind»

In C++ it is possible.
Write the function into the constructor of any class.
Declare/newup a global object of that class.



void fun() { cout<<"fun() called\n"; }

class A {   public: A() {fun();}   };

A obj;

void main()
{
        cout<<"main() called.\n";
}
: 1
« previous next »

 

Best RatedList All>>



Latest
Random



SMF 2.0.10 | SMF © 2015, Simple Machines | Contact Webmaster | OnlineFunDb.com © 2009/10 | Legal Disclaimer