0% found this document useful (0 votes)
62 views6 pages

#Pragma Directive in C

Uploaded by

michal hana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views6 pages

#Pragma Directive in C

Uploaded by

michal hana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

6/17/24, 12:08 AM #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.

Here is the syntax of using a #pragma directive in C/C++ language −

#pragma token_name

Types of Pragma Directives in C


The table of some of #pragma directives in C/C++ language is given as follows,

Directive Description

Before the execution of main(), the function specified in


#pragma startup
pragma is needed to run.

Before the end of program, the function specified in


#pragma exit
pragma is needed to run.

#pragma warn Used to hide the warning messages.

#pragma GCC Checks the dates of current and other file. If other file is
dependency recent, it shows a warning message.

#pragma GCC It treats the code of current file as if it came from


system_header system header.

#pragma GCC poison Used to block an identifier from the program.

#pragma once Compiler loads the header file only once.

#pragma startup and exit


These pragma directives are executed before and after the main() function. Not all
the compilers support these directives.

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();

#pragma startup display


#pragma exit display

int main(){

printf("\nI am in main function");


return 0;
}

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.

The warn pragma is used as per the following syntax −

#pragma warn +xxx (To show the warning)


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

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>

#pragma warn -rvl /* return value */


#pragma warn +par /* parameter never used */
#pragma warn –rch /* unreachable code */

int square(int x){


printf("Hello World");
}

int main(){

square(10);
return 0;
}

Output

When you run this code, it will produce the following output −

Hello World

#pragma GCC poison


The GCC compiler removes an identifier completely from the program. If we want to
block an identifier, then we can use the #pragma GCC poison directive. Its syntax is
as follows −

#pragma GCC poison identifier

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>

#pragma GCC poison printf

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 −

error: attempt to use poisoned "printf"

#pragma GCC dependency


This pragma 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.

Example

Take a look at the following example −

#include <stdio.h>

#pragma GCC dependency "depends.c"

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

The above source code is depending on depends.c. If its compilation timestamp is


more recent, then the following warning is issued −

warning: current file is older than depends.c

#pragma GCC system_header


As a convention, system header files are placed angular brackets in front of the
#include directive, whereas the non-system header files are in quotation marks. If
you want the compiler to treat the latter as the system header, use this pragma.

#pragma GCC system_header

We define a "library.h" header file in the current directory.

#ifndef LIBRARY_H
#define LIBRARY_H

#pragma GCC system_header

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(){

myFunction(); // Using a function from the library.h

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.

Save the "myheader.h" file as −

#pragma once
void myFunction();

In another code (main.c), call myFunction() as follows −

#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

You might also like