Types of pragma directives in C - GeeksforGeeks

04 Apr, 2021

Pragma Directives: The pragma directive is used to control the actions of the compiler in a particular portion of a program without affecting the program as a whole. 
Pragma directives are included in the C program to take effect.
The effect of pragma will be applied from the point where it is included to the end of the compilation unit or until another pragma changes its status.
A #pragma directive is an instruction to the compiler and is usually ignored during preprocessing.
Syntax:
#pragma string
Here, the string can be one of the instructions given to the compiler with any required parameters.         
Types of Pragma Directives:
The syntax of pragma copyright is:
#pragma copyright “string”
Here, the string specifies the set of characters included in the copyright message in the object file.
If no data is specified during pragma copyright then the current year is used in the copyright message:
Example: If copy write is written in a below way:
#pragma copyright “GFG Private Limited”
Then the following string is placed in the object code (assuming the current year is 2020):
© Copyright GFG private limited, 2020.All rights reserved. No part of this program may be copied, reproduced, or transmitted without the prior written consent of GFG Private Limited.
Note: To see the COPYRIGHT string as well as any other strings in the object file, use the strings(1) command with the -a option.
Example:
strings -a ObjectFileName.o
The syntax of pragma copyrighted is:
#pragma COPYRIGHT_DATE “string”
Here, the string is a date that will be used by the COPYRIGHT pragma. Consider the following example given below:  
#pragma COPYRIGHT_DATE “2011-2020” #pragma copyright “GFG Private Limited”
The above pragma will place the following string in the object code:
© Copyright GFG Private Limited, 2011-2020.All right reserved no part of this program may be photocopied reproduced, or transmitted without the prior written consent of GFG private limited.
Note: To see the COPYRIGHT_DATE string as well as any other strings in the object file, use the strings(1) command with the -a option.Example:
strings -a ObjectFileName.o
The syntax of pragma OPT_LEVEL which is used to set the optimization level to 1, 2, 3 or 4 is:
# pragma OPT_LEVEL 1# pragma OPT_LEVEL 2# pragma OPT_LEVEL 3# pragma OPT_LEVEL 4
Like the optimization pragma, even the pragma cannot be used in a function. Finally, OPT_LEVEL 3 and 4 are allowed only at the beginning of the file.
Below is an example code snippet in C illustrating the use of pragma opt level:
C
 

 
 










aCC - O prog.C 
  
#pragma OPT_LEVEL 1 
  
      // Optimise func1() at level 1 
      void
      Func1() 
{ 
} 
  
#pragma OPT_LEVEL 2 
  
// Optimize Func2() to at level 2 
void Func2() 
{ 
}






The syntax of using the pragma OPTIMIZE is:
#pragma OPTIMIZE ON#pragma OPTIMIZE OFF
The pragma optimize is basically used to turn ON/OFF optimization in the section of the source program.
However, when using the pragma specifies one of the optimization options on the ACC command (while giving the command to compile the program), otherwise, this pragma is ignored.
Also, remember that the pragma optimize cannot be used within a function.
Below is an example code snippet in C illustrating the use of optimization pragma:
C++
 

 
 










// Set optimization to level 2 
// for Prog.C 
aCC + O2 Prog.C 
  
#pragma OPTIMISE OFF 
      void Func1 
{ 
    // Turn off optimization for 
    // this function 
} 
  
#pragma OPTIMISE ON 
Void Func2() 
{ 
    // Restore optimization to level 2 
}






The syntax for HP_SHLIB_VERSION which is used to create different versions of shared library routine is:
#pragma HP_SHLIB_VERSION [“]date[“]
Example:
#pragma HP_SHLIB_VERSION [“]12/20[“]
Here, the data argument is of the form month/year, optionally enclosed in quotes.
The month must be specified using any number from 1 to 12.
The year can be specified as either the last two digits of the year (11 for 2011) or a full year specification (2011).
Here are the two-digit real codes from 11 to 20 are used to represent the year from 2011 to 2020, respectively.
The pragma should be used only if incompatible changes are made to a source file.
The syntax of pragma locality which is used to specify the name to be associated with the code that is written to a relocatable object module is:
#pragma LOCALITY “string”
Example:
#pragma LOCALITY “Geeks For Geeks”
In the above example, the string (“Geeks For Geeks”) specifies a name to be used for a coded subspace.
After this directive, all code following the directive is associated with the name specified in the string.
The smallest scope of a unique LOCALITY pragma is a function.
The syntax of pragma VERSION ID can be given as:  
#pragma VERSIONID “string”
Here string is a string of characters that is placed in the object file. 
Example:
#pragma VERSIONID “GFG private limited, Version 1234 5.8.0 1.10”
In the above example, the pragma places the character geeks for GFG private limited, Version 1234 5.8.0 1.10 into the object file.
The pragma once specifies that the file in which this pragma directory is specified will be included opened only once by the compiler in a building of a particular file it’s syntax can be given as: 
#pragma once
Conclusion:
The pragma preprocessor directive is mainly used where each implementation of C supports some features unique to its host machine or operating system.
For example, some programs may need to exercise precise control over the memory areas where data is placed or to control the way certain function receives parameters in such cases, #pragma directives provides operating-system-specific features feature for each compiler while retaining overall compatibility with the C language.

C Macro


C-Macro & Preprocessor


Macro & Preprocessor


C Language


C Programs

Writing code in comment? 
                                    Please use ide.geeksforgeeks.org, 
                                    generate link and share the link here.
                                
TCP Server-Client implementation in C
Exception Handling in C++
'this' pointer in C++
Multithreading in C
Arrow operator -> in C/C++ with Examples
Strings in C
Arrow operator -> in C/C++ with Examples
Header files in C/C++ and its uses
C Program to read contents of Whole File
Basics of File Handling in C