![]() |
VOOZH | about |
In C, the #pragma directive is a special purpose directive that is used to turn on or off some features. #pragma also allows us to provide some additional information or instructions to the compiler. It is compiler-specific i.e., the behavior of pragma directive varies from compiler to compiler.
#pragma directive_nameSome of the commonly used #pragma directives are discussed below:
These directives help us to specify the functions that are needed to run before the program starts ( before the control passes to the main()) and just before the program exits (just before the control returns from the main()).
Syntax
#pragma startup function_name
#pragma exit function_name
Example
The below program will not work with GCC compilers. Look at the below program:
// C program to demonstrate the working of #pragma startup
// and #pragma exit
#include<stdio.h>
voidfunc1();
voidfunc2();
#pragma startup func1
#pragma exit func2
voidfunc1(){printf("Inside func1()\n");}
voidfunc2(){printf("Inside func2()\n");}
intmain()
{
printf("Inside main()\n");
return0;
}
Output
Inside func1()
Inside main()
Inside func2()
The above code will produce the output as given below when run on GCC compilers:
Inside main()
This happens because GCC does not support #pragma startup or exit. However, you can use the below code for a similar output on GCC compilers.
Inside func1() Inside main() Inside func2()
This directive is used to hide the warning messages which are displayed during compilation. This may be useful for us hen we have a large program and we want to solve all the errors before looking on warnings then by using it we can focus on errors by hiding all warnings. we can again let the warnings be visible by making slight changes in syntax.
Syntax
#pragma warn +xxx (To show the warning)
#pragma warn -xxx (To hide the warning)
#pragma warn .xxx (To toggle between hide and show)
We can hide the warnings as shown below:
Example
The below example demonstrates the use of above warnings.
GEEKSFORGEEKS
The above program compiles successfully without any warnings to give the output "GEEKSFORGEEKS".
This directive is supported by the GCC compiler and is used to remove an identifier completely from the program. If we want to block an identifier then we can use the #pragma GCC poison directive.
Syntax
#pragma GCC poison identifierExample
The below example demonstrates the use of #pragma GCC poison.
Output
prog.c: In function 'main':
prog.c:14:9: error: attempt to use poisoned "printf"
printf("GEEKSFORGEEKS");
^
prog.c:17:9: error: attempt to use poisoned "printf"
printf("bye");
^The program will give the above error.
The #pragma GCC dependency allows you to check the relative dates of the current file and another file. If the other file is more recent than the current file, a warning is issued. This is useful if the current file is derived from the other file, and should be regenerated.
Syntax
#pragma GCC dependency "main.c"
Example
In the below program #pragma GCC dependency "parse.y" is used to indicate that this file depends on the file named "parse.y"
Explanation: At the compile time compiler will check whether the "parse.y" file is more recent than the current source file. If it is, a warning will be issued during compilation.
The #pragma GCC system_header directive in GCC is used to tell the compiler that the given file should be treated as if it is a system header file. warnings that will be generated by the compiler for code in that file will be suppressed hence it is extremely useful when the programmer doesn't want to see warnings or errors. This pragma takes no arguments. It causes the rest of the code in the current file to be treated as if it came from a system header.
Syntax
#pragma GCC system_header
Example
The below example demonstrates the use of #pragma GCC system_header.
The above code is for external library that we want to include in our main code. So to inform the compiler that external_library.h is a system header, and we don't want to see warnings from it the #pragma GCC system_header is used.
Output
Hello, Geek!
Explanation: Now inour main program,"external_library.h" header is included and the function declared in it is used. Hello,Geek ! is printed without any warnings.
The #pragma once directive has a very simple concept. The header file containing this directive is included only once even if the programmer includes it multiple times during a compilation. This directive works similar to the #include guard idiom. Use of #pragma once saves the program from multiple inclusion optimisation.
Syntax
#pragma once
Example
The below example demonstrates the use of #pragma once.
Consider the above my_header.h header file in this header file, #pragma once is used to guard against multiple inclusions.
Output
Hello, World!