Table of contents
1.
Introduction
2.
#pragma Directives
2.1.
#pragma exit and #pragma startup:-
2.2.
#pragma warn:-
2.3.
#pragma GCC poison:-
2.4.
#pragma GCC dependency:-
2.5.
#pragma GCC system_header:-
2.6.
#pragma once:-
3.
Frequently Asked Questions
3.1.
What are pragma directives?
3.2.
What does #pragma warn does?
3.3.
Why are #pragma startup and #pragma exit used?
4.
Conclusion
Last Updated: Mar 27, 2024

C #Pragma

Author Aditi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A pragma directive is a special purpose directive used to turn some features on or off. These types of directives are compiler-specific, i.e., they vary from compiler to compiler. The compiler uses the #pragma directive to offer machine or operating-system features.

Syntax:

#pragma token  

Different compilers may use the #pragma directive in different ways. #pragma directives that are supported by the turbo C++ compiler are given below:-

  1. #pragma argsused  
  2. #pragma hdrfile
  3. #pragma startup 
  4. #pragma exit  
  5.  #pragma inline 
  6. #pragma hdrstop  
  7. #pragma option  
  8. #pragma warn
  9. #pragma saveregs  

Also see: C Static Function, and Tribonacci Series

#pragma Directives

#pragma exit and #pragma startup:-

These directives allow us to declare the functions that must execute before the program starts (before the control goes to the main()) and before the program ends (before the control returns from the main()).

Program:

#include<stdio.h>  
#include<conio.h>  
 
void myFunc() ;  
 
#pragma startup myFunc  
#pragma exit myFunc  
 
void main(){  
    printf("\nI am in main");  
    getch();  
}  
 
void myFunc(){  
    printf("\nI am in myFunc");  
    getch();  
}
You can also try this code with Online C Compiler
Run Code

Output:

I am in myFunc
I am in main
I am in myFunc
You can also try this code with Online C Compiler
Run Code

#pragma warn:-

This directive is used to conceal the compilation of warning messages. When we have a vast program and want to fix all mistakes before looking at warnings, we may use this to hide all warnings and focus on the faults. By making minor syntactic adjustments, we may make the warnings visible once again.

Syntax:

#pragma warn +xxx (To show the warning)
#pragma warn -xxx (To hide the warning)
#pragma warn .xxx (To toggle between hide and show)

Program:

#include<stdio.h>
 
#pragma warn -rvl /* return value */
#pragma warn -par /* parameter never used */
#pragma warn -rch /*unreachable code */
       
int show(int x)
{  
    // x parameter is never used in the function
    //and it does not have a return statement
     
    printf("CodingNinjas");
}
           
int main()
{
    show(5);
    return 0;
}

Output:

CodingNinjas

#pragma GCC poison:-

The GCC compiler supports this command, which is used to entirely delete an identifier from a program. The #pragma GCC poison directive can prevent an identifier from being used.

Program:

#include<stdio.h>
 
#pragma GCC poison printf
 
int main()
{
    int x=5;
     
    if(x==5)
    {
        printf("CodingNinjas");
    }
    else
        printf("All Good");
 
    return 0;
}

Output:

prog.c: In function 'main':
prog.c:14:9: error: attempt to use poisoned "printf"
         printf("CodingNinjas");
         ^
prog.c:17:9: error: attempt to use poisoned "printf"
         printf("All Good");
         ^

#pragma GCC dependency:-

You may examine the relative dates of the current file and another file with the #pragma GCC dependency. A warning is sent if the other file is more recent than the existing file. This is handy if the current file is derived from the additional file and has to be regenerated.

Syntax:

#pragma GCC dependency "parse.y"
#pragma GCC dependency "/usr/include/time.h" rerun fixincludes

#pragma GCC system_header:-

This pragma does not accept any arguments. The rest of the code in the current file is processed as though it originated from a system header.

#pragma once:-

The principle behind the #pragma once the directive is highly straightforward. Even if the programmer includes it numerous times during compilation, the header file containing this directive is only included once. There is no mention of this in any ISO C++ standard. The #include guard idiom is comparable to this directive. The use of #pragma just once prevents the program from being optimized for numerous inclusions.

Syntax:

#pragma once

 

You can also read about dynamic array in c and Short int in C Programming

Frequently Asked Questions

What are pragma directives?

A pragma directive is a special purpose directive used to turn some features on or off. The compiler uses the #pragma directive to offer machine or operating-system features.

What does #pragma warn does?

#pragma warn directive is used to conceal the compilation warning messages.

Why are #pragma startup and #pragma exit used?

These directives allow us to declare the functions that must execute before the program starts (before the control goes to the main()) and before the program ends (before the control returns from the main()).

Conclusion

In this article, we have extensively discussed the C/C++ programming language's pragma directive concepts.

We hope this blog has helped you enhance your knowledge regarding the pragma directives. Some official documentation on the C programming language that can help you improve your understanding is C documentation and Pragmas.

If you would like to learn more, check out our articles on Tokens in C/C++C/C++ interview questions, and pointers in C

Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass