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.
Syntax of #pragma
#pragma directive_name
Commonly Used #pragma Directives in C
Some of the commonly used #pragma directives are discussed below:
1. #pragma startup and #pragma exit
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
// C program to demonstrate the working of #pragma startup
// and #pragma exit
#include <stdio.h>
void func1();
void func2();
#pragma startup func1
#pragma exit func2
void func1() { printf("Inside func1()\n"); }
void func2() { printf("Inside func2()\n"); }
int main()
{
printf("Inside main()\n");
return 0;
}
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.
C
// C program to demonstrate the working of #pragma startup
// and #pragma exit (without GCC compiler)
#include <stdio.h>
void func1();
void func2();
void __attribute__((constructor)) func1();
void __attribute__((destructor)) func2();
void func1() { printf("Inside func1()\n"); }
void func2() { printf("Inside func2()\n"); }
int main()
{
printf("Inside main()\n");
return 0;
}
OutputInside func1()
Inside main()
Inside func2()
2. #pragma warn Directive
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:
- #pragma warn -rvl: This directive hides those warning which are raised when a function which is supposed to return a value does not return a value.
- #pragma warn -par: This directive hides those warning which are raised when a function does not uses the parameters passed to it.
- #pragma warn -rch: This directive hides those warning which are raised when a code is unreachable. For example: any code written after the return statement in a function is unreachable.
Example
The below example demonstrates the use of above warnings.
C
// C program to explain the working of
// #pragma warn directive
// This program is compatible with C/C++ compiler
#include<stdio.h>
#pragma warn -rvl /* return value */
#pragma warn -par /* parameter never used */
#pragma warn -rch /*unreachable code */
int show(int x)
{
// parameter x is never used in
// the function
printf("GEEKSFORGEEKS");
// function does not have a
// return statement
}
int main()
{
show(10);
return 0;
}
The above program compiles successfully without any warnings to give the output "GEEKSFORGEEKS".
3. #pragma GCC poison
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 identifier
Example
The below example demonstrates the use of #pragma GCC poison.
C
// C Program to illustrate the
// #pragma GCC poison directive
#include <stdio.h>
#pragma GCC poison printf
int main()
{
int a = 10;
if (a == 10) {
printf("GEEKSFORGEEKS");
}
else
printf("bye");
return 0;
}
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.
4. #pragma GCC dependency
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"
C
// C program to demonstrate use of
#include <stdio.h>
// Using #pragma GCC dependency to check if "parse.y" file
// has been modified
#pragma GCC dependency "solution.c"
int main()
{
printf("Hello, Geek!");
return 0;
}
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.
5. #pragma GCC system_header
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.
C
// external_library.h
#ifndef EXTERNAL_LIBRARY_H
#define EXTERNAL_LIBRARY_H
#pragma GCC system_header
void externalFunction();
#endif
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.
C
// C program to demonstrate #pragma GCC system_header
#include <stdio.h>
#include "external_library.h"
int main() {
externalFunction(); // Using a function from the external library
printf("Hello, Geek!\n");
return 0;
}
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.
6. #pragma once
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.
C
// my_header.h
#pragma once
void myFunction();
Consider the above my_header.h header file in this header file, #pragma once
is used to guard against multiple inclusions.
C
// C program to demonstrate the use of pragma once
#include <stdio.h>
#include "my_header.h"
int main() {
myFunction(); // Using a function from my_header.h
printf("Hello, World!\n");
return 0;
}
Output
Hello, World!
Similar Reads
C Preprocessor Directives
In C programming, the preprocessor is a program that process the source code before the actual compilation begins. It uses preprocessor directives are commands that instruct the preprocessor to perform specific actions. These directives start with the # symbol.List of Preprocessor DirectivesThe foll
6 min read
dot (.) Operator in C
In C, the dot (.) operator is used to access members of user defined data types such as a structure or union. Also known as the direct member access operator, it allows us to access the data of the struct and union via the name of variables.Let's take a look at an example:C#include <stdio.h> s
2 min read
#define in C
In C programming, #define is a preprocessor directive that is used to define macros. The macros are the identifiers defined by #define which are replaced by their value before compilation. We can define constants and functions like macros using #define. The generics in C are also implemented using t
3 min read
Debugging Using Preprocessor Directive in C
The C-compiler can detect only syntax errors whereas it cannot detect any run-time errors. So, this may produce an erroneous output if the run-time error persists. To detect the run-time errors, the programmer needs to debug the program before execution. But this debugging process becomes a quite te
3 min read
User-Defined Function in C
A user-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User-defined functions are different from built-in functions as their working is specified by the user and no hea
6 min read
Derived Data Types in C
Data types in the C language can be categorized into three types, namely primitive, user-defined, and derived data types. In this article, we shall learn about derived data types. In C, the data types derived from the primitive or built-in data types are called Derived Data Types. In other words, th
4 min read
strtoumax() function in C++
The strtoumax() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as an uintmax_t(maximum width unsigned integer). This function also sets an end pointer that points to the first character after the last valid numeric character of th
4 min read
raise() function in C++
csignal header file declared the function raise() to handle a particular signal. Signal learns some unusual behavior in a program, and calls the signal handler. It is implemented to check if the default handler will get called or it will be ignored. Syntax: int raise ( int signal_ ) Parameter: The f
3 min read
putchar() function in C
The putchar(int ch) method in C is used to write a character, of unsigned char type, to stdout. This character is passed as the parameter to this method. Syntax: int putchar(int ch) Parameters: This method accepts a mandatory parameter ch which is the character to be written to stdout. Return Value:
1 min read
fwrite() in C
In C, fwrite() is a built-in function used to write a block of data from the program into a file. It can write arrays, structs, or other data to files but is especially designed to write binary data in binary files.Example:C#include <stdio.h> int main() { FILE *fptr = fopen("gfg.bin", "wb"); i
3 min read