Saturday, August 21, 2010

Execute function before execution of main() function in C using #pragma directive

/*This Program demonstrates the use of #pragma directive.
  #pragma startup can be used to call a function before main.The function
  must not returning anything other than void and must not contain any
  arguments.
  
  Syntax : #pragma startup function-name [priority]
  Here function-name is the name of a function and priority is optional.
  priority can be from 64 to 255.Do not use priority from 0 to 63 because
  they are used by C libraries.
  100 is a Default priority
  255 is a lowest priority
  Developed on       :- 28/03/2009
  Developed by       :- Malhar Vora 
  Email              :- vbmade2000@gmail.com         
  WebSite            :- http://malhar2010.blogspot.com
  Development Status :- Completed
  
******************************************************************/


 #include 

void PrintChar1()
{
   printf("Hello before main1\n");
}

void PrintChar2()
{

   printf("Hello before main2\n");
}


#pragma startup PrintChar1 64
#pragma startup PrintChar2 65

void main()
{

   printf("Hello from Main");

}

7 comments:

  1. i executed but i doesnt... the output i got is Hello from Main

    ReplyDelete
  2. It doesnt work...please update your post and say if this is possible in c?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. This comment has been removed by the author.

      Delete
  3. #include
    #include
    void function();
    #pragma startup function
    void main()
    {
    printf("\n\tThis message is from main");
    getch();
    }
    void function()
    {
    clrscr();
    printf("\n\tThis message is from function before executing main");
    }
    Hope this program helps you..in this program function executes before executing main...just run it & you will understand..-Debayan Basu

    ReplyDelete

Note: Only a member of this blog may post a comment.