C-#pragma .

In this session i have discussed #pragma preprocessor in C .


The #pragma is very special preprocessor . It is used to execute the code before main() function execute . It is vary from compiler to compiler .


The below example work on turbo compiler .

Example : Write a program that demonstrate the use of #pragma preprocessor .

    #include<stdio.h>
    void show1();
    void show2();

    #pragma startup show1
    #pragma exit show2

    void show1()
    {
        printf("This is show1 \n",s);
    }

    void show2()
    {
       printf("This is show2 \n",s);
    }

    int main()
    {
        printf("Inside main()\n");

        return 0;
    }
    
    OUTPUT : This is show1
             Inside main()
             This is show2
    

The below code work on GCC compiler .

Example : Write a program that demonstrate the #pragma .

    #include<stdio.h>
    void show1();
    void show2();

    void __attribute__((constructor)) show1();
    void __attribute__((destructor)) show2();

    void show1()
    {
        printf("This is show1 \n",s);
    }

    void show2()
    {
       printf("This is show2 \n",s);
    }

    int main()
    {
        printf("Inside main()\n");

        return 0;
    }
    
    OUTPUT : This is show1
             Inside main()
             This is show2
    

Welcome to AndroidATPoint.

back image                     next image