Procecessor Directive : #pragma

The pragma directive is used to access compiler-specific preprocessor extensions. It is basically used to provide additional information to the compiler. The syntax is :
 #pragma token
The Turbo C compiler supports following types of pragma tokens

#pragma startup
#pragma exit
#pragma warn
#pragma argsused 
#pragma hdrfile
#pragma hdrstop
#pragma inline
#pragma option
#pragma saveregs

#pragma startup :

The #pragma startup allows the C program to specify function(s) that should be called upon program startup. The syntax is :
 #pragma startup <function_name> <Priority>
Where :
  • function_name should be the name of function, and also note that the function must be previously declared that takes no arguments and return void. for example :
void func(void);
  • priority is an optional parameter, which consists an integer value ranging from 64 to 255. For example the values are :
        64 : First available user priority
        100 : Default priority
        255 : Lowest priority

functions with higher priority are called first at startup.

Example :
 #pragma startup MyFunc 110
Now lets see an Example C program :
#include <stdio.h>
#include <conio.h>

void MyFunc();

#pragma startup MyFunc 105

int main() {
  printf("Hello I am the main function.\n");
  getch(); 
  return 0;
}

void MyFunc() {
  printf("Hello world\n");
  printf("Called by '#pragma startup'\n\n");
}
Output :

Hello world
Called by '#pragma startup'

Hello i am the main function.

#pragma exit :

The #pragma exit allows us to specify functions called upon program exit. The syntax is :
 #pragma startup <function_name> <Priority>
Example C program :
#include <stdio.h>
#include <conio.h>

void MyFunc();

#pragma exit MyFunc 105

int main() {
  printf("Hello I am the main function.\n\n");
  getch();
  return 0;
}

void MyFunc() {
  printf("Hello world\n");
  printf("Called by '#pragma exit'\n");
  getch();
}
Output :

Hello I am the main function.

Hello world
Called by '#pragma exit'


Next : C Language Tutorials List