Thursday, February 25, 2016

#pragma Pre-processor Directive

Pragma is used to turn on or off some of compiler/system features. Pragma's operation is system and compiler dependent. So, all pragmas may not work with all the compilers. When pragmas are encountered, the default behaviour of the compiler is overwritten by the options provided by pragmas. One of such pragma is 'pragma startup' and 'pragma exit'. The pragma startup is used to specify function that will be called before main function and pragma exitis used to specify function that would be called just before the program exits. Consider the example program

Pragma Example in C

  1. #include<stdio.h>
  2.  
  3. void function1();
  4. void function2() ;
  5.  
  6. #pragma startup function1
  7. #pragma exit function2
  8.  
  9. void main()
  10. {
  11. printf("In main function");
  12. }
  13.  
  14. void function1()
  15. {
  16. printf("In function1\n");
  17. }
  18.  
  19. void function2()
  20. {
  21. printf("In function2");
  22. }
  23.  
  24. Output:
  25. In function1
  26. In main function
  27. In function2 .

No comments: