GCC supports construtors/destructor functions which support running function before or after main():
The
constructor
attribute causes the function to be called automatically before execution entersmain()
. Similarly, thedestructor
attribute causes the function to be called automatically aftermain()
completes orexit()
is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program.
Here is an example from GeeksforGeeks.
When is the proper scenario of using this feature? Especially a function to be called before main(), what is the difference if we just place it in start of main()?