How can i change the priority of function?
I want to run nay other function before execution of main() function.

You can't. The main() function is, by definition, the first function your program runs.

What difference does it make? Just call your function first thing in main:

int main()
  {
  my_init_func();

  ...

Hope this helps.

no man I am talking about execution. In above case first main will execute and then my_init_func() will be called.I dont want that.

See in following case

int main()
{
cout<<"In main"<<endl;
}
my_function()
{
cout<<"In my function";
}

O/P should be:
In my function
In main

no man I am talking about execution. In above case first main will execute and then my_init_func() will be called.I dont want that.

See in following case

int main()
{
cout<<"In main"<<endl;
}
my_function()
{
cout<<"In my function";
}

O/P should be:
In my function
In main

Duoas was talking about execution order too. I can't think of any possible way to run the above program and get the results you want. The program above won't even compile.

main() is the point of entry to your program .. so there is no way around it. I am not even sure why you want to do this ?

You might try something like ...

#include <iostream>
using namespace std;
struct test
{
	void myfunction()
	{
		cout << "myfunction()\n";
	}

	test()
	{
		myfunction();
	}
};

static test testing;

int myfunction2()
{
	cout << "myfunction2\n" << endl;
	return 43;
}

static int test2 = myfunction2();

int main(void)
{
	cout << "main()\n";

	cin.get();

	return 0;
}

I'd be interested to know why is this important to you?

[EDIT]
However, the safest way is simply to call your function first thing in the main(), i.e. not relying on the order of initialization.

commented: Impressive. +7

You might try something like ...

#include <iostream>
using namespace std;
struct test
{
	void myfunction()
	{
		cout << "myfunction()\n";
	}

	test()
	{
		myfunction();
	}
};

static test testing;

int myfunction2()
{
	cout << "myfunction2\n" << endl;
	return 43;
}

static int test2 = myfunction2();

int main(void)
{
	cout << "main()\n";

	cin.get();

	return 0;
}

I'd be interested to know why is this important to you?

Wow. Cool. Learn something new every day!

However take into account the C++ Standard, 3.6.2 (3):

It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized.[Example:
// - File 1 -
# include "a.h"
# include "b.h"
B b;
A::A() {
b.Use ();
}
// - File 2 -
# include "a.h"
A a;
// - File 3 -
# include "a.h"
# include "b.h"
extern A a;
extern B b;
int main () {
a.Use ();
b.Use ();
}
It is implementation-defined whether either a or b is initialized before main is entered or whether the initializations are delayed until a is first used in main. In particular, if a is initialized before main is entered, it is not guaranteed that b will be initialized before it is used by the initialization of a, that is, before A::A is called. If, however, a is initialized at some point after the first statement of main, b will be initialized prior to its use in A::A. —end example]

Some definitions from 3.6.2 (1):

Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. A reference with static storage duration and an object of POD type with static storage duration can be initialized with a constant expression (5.19); this is called constant initialization. Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. Static initialization shall be performed before any dynamic initialization takes place.

I don't know compilers which perform deferred static objects initialization (and where this well-known trick does not work) but these compilers are standard-conformed...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.