Call a function before main
You can have a global variable or a static
class member.
1) static
class member
//BeforeMain.h
class BeforeMain
{
static bool foo;
};
//BeforeMain.cpp
#include "BeforeMain.h"
bool BeforeMain::foo = foo();
2) global variable
bool b = foo();
int main()
{
}
Note this link - Mirror of http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14 / proposed alternative - posted by Lundin.
Is it possible to call a function without calling main function?
The C99 and following C11 (see n1570, its latest draft, which is actually the standard in practice) standards define two flavors of C implementations (see C syntax wikipage) .
- hosted implementations (useful to code application software, e.g. on Linux, POSIX, or probably Windows) give you a standard C library (
libc
) - givingmalloc
from<stdlib.h>
andprintf
from<stdio.h>
etc etc..., and your program should define amain
function (of signatureint main(int, char**)
or justint main(void)
). In a hosted implemention most functions are indirectly called frommain
. As John Zwinck answered, some compilers give you a way to define functions to be called beforemain
in a very implementation specific way (and order). Notice that thelibc
usually requires some implementation specific initialization and yourmain
is actually called from crt0 in an implementation specific way. - freestanding implementations (useful to code system kernel software, or embedded software on micro-controllers) don't provide a full
libc
and do not define how the code can be run (and started). In that case your implementation should define how the software is run. In practice you'll need some external (e.g. assembly) code to call functions from your code.
The GCC compiler accepts the -ffreestanding
flag to give you a freestanding implementation (otherwise it is hosted)
Notice that hosted implementations are permitted to compile some standard functions in a tricky and magic way (if you #include
the standard header defining them). See examples here.
Notice also that the standard is defining how main
works in a hosted implementation (§5.1.2.2. Hosted environment page 12 of n1570
). In particular main
is the only function where the lack of return
is the same as return 0;
(also the standard exit(3) function would end the program nearly as if returning from main
).
In practice your question is implementation specific.
Executing code before main()
There are ways using __attribute__
but those are very specific to your compiler and code that is written using these are not really portable. On the other hand, the C language does not provide any start-up modules/libraries.
In C, logically main()
is the first function called by the OS. But before calling main()
, the OS calls another function called start-up
module to setup various environment variables, initialize (un-initialized) static variables, build a stack frame (activation record) and initialize the stack pointer to the start of the stack area and other tasks that have to be done before calling main()
.
Say if you are writing code for embedded systems where there is no-or-minimal OS to do the above mentioned work, then you should explore these options which are compiler dependent. Other than GCC, Turbo-C and Microsoft C compilers provides facilities to add code in a particular hardware machine (f.e. 8086 machines).
In other words, the start-up modules are not meant for the programmers.
Define a function before main?
How and where to prototype and define a function in C :
Your function is used only in a specific .c file :
Define it static in the .c file. The function will only be visible and compiled for this file.Your function is used in multiple .c files :
Choose an appropriate c file to host your definition (All foo related functions in a foo.c file for example), and have a related header file to have all non-static (think public) functions prototyped. The function will be compiled only once, but visible to any file that includes the header files. Everything will be put together at link time. Possible improvement : always make the related header file, the first one included in its c file, this way, you will be sure that any file can include it safely without the need of other includes to make it work, reference : Large Scale C++ projects (Most of the rules apply to C too).Your function is inlinable (are you sure it is ?) :
Define the function static inline in an appropriate header file. The compiler should replace any call to your function by the definition if it is possible (think macro-like).
The notion of before-after another function (your main function) in c is only a matter of style. Either you do :
static int foo(int foo)
{
// code
return 1;
}
int main(void)
{
foo(1);
return 0;
}
Or
static int foo(int foo);
int main(void)
{
foo(1);
return 0;
}
static int foo(int foo)
{
// code
return 1;
}
will result in the same program. The second way is prefered by programmers because you don`t have to reorganize or declare new prototypes every time you declare a new function that use the other ones. Plus you get a nice list of every functions declared in your file. It makes life easier in the long run for you and your team.
Why can't a function go after Main
You can, but you have to declare it beforehand:
void myFunction(); // declaration
int main()
{
myFunction();
}
void myFunction(){} // definition
Note that a function needs a return type. If the function does not return anything, that type must be void
.
Can we call functions before defining it?
C89 allow this by implicitly converting the return type of function and parameter passed to it to int
. See here.
But, this is not valid in C99 and later. This has been omitted from the standard. Either you have to declare a prototype for your function or define it before main
. See the result here. There is a compile time error in this case.
Is it possible to call a function outside of main()?
It's possible, but unnecessary.
Instead, wrap it in a class that closes it in the destructor, like you did with the other objects.
Destructors are called in the reverse order, which means that if you create the display first, it'll die last.
The way you would've called it after main
is, similarily, from a destructor of a global or function-local static
object. A function-local static is better than a global variable because it avoids the static init order fiasco.
Related Topics
How to Use Std::Sort with a Vector of Structures and Compare Function
What Are the Issues with a Vector-Of-Vectors
Parse Quoted Strings with Boost::Spirit
Issuing System Commands in Linux from C, C++
Why Is This Cin Reading Jammed
Is It a Good Idea to Wrap an #Include in a Namespace Block
Why Is This Code Trying to Call the Copy Constructor
What Is the Value Category of the Operands of C++ Operators When Unspecified
What Does '<Cuchar>' Provide, and Where Is It Documented
Invalid Conversion from 'Const Char*' to 'Char'
Access Array Beyond the Limit in C and C++
Allowing Signed Integer Overflows in C/C++
Multi-Threading Benchmarking Issues