#pragma startup and #pragma exit directive in c Programming Language



#pragma startup and #pragma exit directive in c Programming Language

Syntax : startup pragma

#pragma startup <function_name> [priority]
  • startup pragma allow the program to specify function(s) that should be called upon program startup
  • Function is called before main().

Syntax : exit pragma

#pragma exit <function_name> [priority]
  • exit pragma allow the program to specify function(s) that should be called just before the program terminates through _exit.
  • Function is called after main().

<function_name>

  • <function_name> must be a previously declared function that takes no arguments and returns void.
  • It should be declared as –
void func(void);
  • The function name must be defined (or declared) before the pragma line is reached.

[priority]

0    = Highest priority
0-63 = Used by C libraries
64   = First available user priority
100  = Default priority
255  = Lowest priority

Note :

  1. The optional priority parameter is an integer in the range 64 to 255.
  2. Do not use priorities from 0 to 63.
  3. 0-63 are used by the C libraries.
  4. Functions with higher priorities are called first at startup and last at exit.

Live Example

#include<stdio.h>
#include<conio.h>

void School();
void College() ;

#pragma startup School 105
#pragma startup College
#pragma exit College
#pragma exit School 105

void main(){
printf("\nI am in main");
getch();
}

void School(){
printf("\nI am in School");
getch();
}

void College(){
printf("\nI am in College");
getch();
}

Output :

I am in College
I am in School
I am in main
I am in School
I am in College