What is Main Function in C ?

What is Main Function in C ?


here are few ways to use a function and execute some statements before and after main()

With GCC family of C compilers, we can mark some functions to execute before and after main(). So some startup code can be executed before main() starts, and some cleanup code can be executed after main() ends. For example, in the following program, startupfun() is called before main() and cleanupfun() is called after main().

     #include<stdio.h>
    /* Apply the constructor attribute to startupfun() so that it is executed before main() */
    void startupfun (void) __attribute__ ((constructor));

    /* Apply the destructor attribute to cleanupfun() so that it is executed after main() */
    void cleanupfun (void) __attribute__ ((destructor));

    void startupfun (void)
    {    printf ("Startup method before main()\n");   }

    void cleanupfun (void)
    {    printf ("Cleanup method after main()\n");    }

    int main (void)
    {
        printf ("Hi! Check This Out.\n");
        return 0;
    }

Got to learn this from here.


Using #pragma directive for special purpose of turning certain features on or off has been a practice. Though the same can be used with Turbo C/C++ compiler [compiler dependent] to allow a function to start before and end after main(). More than one function called before main should be defined in the reverse order of required execution.

    #include<stdio.h>
    void fun_before();
    void fun_after();

 /`* Initializing methods for compiler to deal with at startup in reverse order and exit point of main method.  */`

     #pragma startup fun_before
     #pragma startup fun_start
     #pragma exit fun_after

    int main()