#pragma Directive: C Program

In this video tutorial lets look at using #pragma preprocessor directive.

#pragma Directive

#pragma is a special purpose directive which can be used to turn on and off certain features.

Note: #pragma commands or keywords vary from one compiler to another. So make sure to check the keywords and/or syntax for the compiler you are using.

Video Tutorial: #pragma Preprocessor Directive: C Program



YouTube Link: https://www.youtube.com/watch?v=3jsqavQ5faU [Watch the Video In Full Screen.]

Source Code: #pragma Directive: C Program

  1. #include<stdio.h>  
  2.   
  3. void init();  
  4. void end();  
  5.   
  6. #pragma startup init  
  7. #pragma exit end  
  8.   
  9. int main()  
  10. {  
  11.     printf("We're inside main method\n");  
  12.   
  13.     return 0;  
  14. }  
  15.   
  16. void init()  
  17. {  
  18.     printf("We're inside init method\n");  
  19. }  
  20.   
  21. void end()  
  22. {  
  23.     printf("We're inside end method\n");  
  24. }  

Output:
We’re inside init method
We’re inside main method
We’re inside end method

#pragma startup and #pragma exit are keywords.

#pragma startup followed by a function name: this makes the function name specified to be executed before main method.

#pragma exit followed by a function name: this makes the function name specified to be executed just before program termination.

Note:
#pragma startup and #pragma exit doesn’t work in GCC compiler. So the equivalent behavior can be obtained by using below syntax:

Source Code: For GCC Compiler: C Program

  1. #include<stdio.h>  
  2.   
  3. void __attribute__((constructor)) init();  
  4. void __attribute__((destructor)) end();  
  5.   
  6. int main()  
  7. {  
  8.     printf("We're inside main method\n");  
  9.   
  10.     return 0;  
  11. }  
  12.   
  13. void init()  
  14. {  
  15.     printf("We're inside init method\n");  
  16. }  
  17.   
  18. void end()  
  19. {  
  20.     printf("We're inside end method\n");  
  21. }  

Output:
We’re inside init method
We’re inside main method
We’re inside end method

__attribute__((constructor)) acts like #pragma startup and __attribute__((destructor)) acts like #pragma exit.

  1. #include<stdio.h>  
  2.   
  3. void init();  
  4. void end();  
  5.   
  6. int main()  
  7. {  
  8.     printf("We're inside main method\n");  
  9.   
  10.     return 0;  
  11. }  
  12.   
  13. void __attribute__((constructor)) init()  
  14. {  
  15.     printf("We're inside init method\n");  
  16. }  
  17.   
  18. void __attribute__((destructor)) end()  
  19. {  
  20.     printf("We're inside end method\n");  
  21. }  

Output:
We’re inside init method
We’re inside main method
We’re inside end method

We could even use __attribute__((constructor)) and __attribute__((destructor)) in function definition, as shown in above source code.

Source Code: Suppressing warnings using #pragma Directive: C Program

  1. #include<stdio.h>  
  2.   
  3. #pragma warn -rvl /* No Return Value */#pragma warn -par /* Parameter Not Used */#pragma warn -rch /* Unreachable Code */  
  4. int main()  
  5. {  
  6.     int count = 1;  
  7.   
  8.     printf("%d\n", count);  
  9.   
  10.     return 0;  
  11.   
  12.     count++;  
  13.   
  14. }  
  15.   
  16. int total()  
  17. {  
  18.     printf("Sum of a and b is c\n");  
  19. }  
  20.   
  21. void end(int x)  
  22. {  
  23.     printf("We're inside end method\n");  
  24. }  

Output:
1

Here function total has a return type of integer, but it doesn’t return any value from inside, so it must throw “no return value” warning. But that warning is suppressed by #pragma warn -rvl .

Function end takes a integer argument, but the value is nowhere used inside the function definition. Compiler should throw “Parameter Not Used” warning, but it’s suppressed by #pragma warn -par.

Inside main method, there is count++ after return. This code is never executed. So the compiler should throw “Unreachable Code” warning, which is suppressed by #pragma warn -rch.

Note: -(minus or subtraction) symbol before rvl, par and rch means compiler removes the warning. + sign indicated the warning will be shown in the editor after compilation of the program.

Also note that,

  1. #pragma warn -rvl /* No Return Value */#pragma warn -par /* Parameter Not Used */#pragma warn -rch /* Unreachable Code */  

doesn’t work on GCC compiler.

Important Note: The functions init() and end() should not receive any arguments and should not return any value, if we want to handle their invocation via #pragma.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert