#Pragma Directive in C
#Pragma Directive in C
#pragma Directive in C
The preprocessor directive #pragma is used to provide additional information to the
compiler in C/C++ language. This is used by the compiler to provide some special
features.
Note that pragmas are compiler dependent. Not all the pragma directives are
supported by all the compilers.
#pragma token_name
Directive Description
#pragma GCC Checks the dates of current and other file. If other file is
dependency recent, it shows a warning message.
https://www.tutorialspoint.com/cprogramming/c_pragmas.htm 1/6
6/17/24, 12:08 AM #pragma Directive in C
Example
The following code demonstrates how you can use the pragma startup and exit
directives −
#include <stdio.h>
int display();
int main(){
int display() {
printf("\nI am in display function");
return 0;
}
Output
When you run this code, it will produce the following output −
I am in main function
#pragma warn
The #pragma warn directive is used to hide or display the warning messages which
are displayed during compilation.
https://www.tutorialspoint.com/cprogramming/c_pragmas.htm 2/6
6/17/24, 12:08 AM #pragma Directive in C
The three character codes to be used are rvl (return value), par (parameter used or
not), and rch (if the code is unreachable).
If any character code is prefixed by "+", it indicates to show the warning; prefixed
by "–" means indication to the compiler to hide warnings, and prefix by dot (.) is an
instruction to the compiler to toggle between hide and display warnings.
Example
The following example shows how you can use the warn pragma in a C program −
#include <stdio.h>
int main(){
square(10);
return 0;
}
Output
When you run this code, it will produce the following output −
Hello World
https://www.tutorialspoint.com/cprogramming/c_pragmas.htm 3/6
6/17/24, 12:08 AM #pragma Directive in C
Example
In this example, we will use the GCC poison pragma to block the printf() statement
−
#include <stdio.h>
int main(){
int a = 10;
if (a == 10) {
printf("Hello World");
}
else
printf("TutorialsPoint");
return 0;
}
Output
When you try to compile this code, it will show the following error −
Example
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_pragmas.htm 4/6
6/17/24, 12:08 AM #pragma Directive in C
printf("Hello, World!");
return 0;
}
Output
#ifndef LIBRARY_H
#define LIBRARY_H
void myFunction();
#endif
To ask the compiler to treat "library.h" as a system header, use the #pragma GCC
system_header.
#include <stdio.h>
#include "library.h"
int main(){
https://www.tutorialspoint.com/cprogramming/c_pragmas.htm 5/6
6/17/24, 12:08 AM #pragma Directive in C
printf("Hello, World!\n");
return 0;
}
#pragma once
The "#pragma once" directive causes the header file to be included only once, even
if the programmer includes it multiple times.
#pragma once
void myFunction();
#include <stdio.h>
#include "myheader.h"
int main(){
myFunction();
printf("Hello, World!\n");
return 0;
}
https://www.tutorialspoint.com/cprogramming/c_pragmas.htm 6/6