C – Pragmas


#pragma Directive in C



”;


What is #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.

Syntax

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
#pragma startup Before the execution of main(), the function specified in pragma is needed to run.
#pragma exit Before the end of program, the function specified in pragma is needed to run.
#pragma warn Used to hide the warning messages.
#pragma GCC dependency Checks the dates of current and other file. If other file is recent, it shows a warning message.
#pragma GCC system_header It treats the code of current file as if it came from 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.

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)

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

Example

In this example, we will use the GCC poison pragma to block the printf() function


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

   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.

Syntax


#pragma GCC system_header

library.h

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


#ifndef LIBRARY_H
#define LIBRARY_H

#pragma GCC system_header
 
void myFunction();

#endif

Example

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

   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 −

myheader.h


#pragma once
void myFunction();

Example

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


#include <stdio.h>
#include "myheader.h"

int main(){

   myFunction();
   printf("Hello, World!n");
   return 0;
}

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *