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:-
- #pragma argsused
- #pragma hdrfile
- #pragma startup
- #pragma exit
- #pragma inline
- #pragma hdrstop
- #pragma option
- #pragma warn
- #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();
}
Output:
I am in myFunc
I am in main
I am in myFunc
#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