Open In App

Types of pragma directives in C

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. 

Syntax:



#pragma string

Here, the string can be one of the instructions given to the compiler with any required parameters.         



Instruction Description
COPYRIGHT To specify a copyright string
COPYRIGHT_DATE To specify a copyright date for the copyright string
OPTIMIZE  To turn the optimization feature on or off
LOCALITY  To name a coded subspace
OPT_LEVEL To set the level of optimization
HP_SHLIB_VERSION To create a version of a shared library routine
VERSION ID To specify a version string
ONCE Specify that file opened only once

Types of Pragma Directives:

pragma COPYRIGHT:

The syntax of pragma copyright is:

#pragma copyright “string”

Example: If copy write is written in a below way:

#pragma copyright “GFG Private Limited”

© 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

pragma COPYRIGHT_DATE:

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

pragma OPT_LEVEL:

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:




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

pragma OPTIMIZE:

The syntax of using the pragma OPTIMIZE is:

#pragma OPTIMIZE ON
#pragma OPTIMIZE OFF

Below is an example code snippet in C illustrating the use of optimization pragma:




// 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
}

pragma HP_SHLIB_VERSION:

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[“]

pragma LOCALITY:

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”

pragma VERSION ID:

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.

pragma once:

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:


Article Tags :