Skip to content
Related Articles

Related Articles

Improve Article

Types of pragma directives in C

  • Last Updated : 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.


#pragma string

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

COPYRIGHTTo specify a copyright string
COPYRIGHT_DATETo specify a copyright date for the copyright string
OPTIMIZE To turn the optimization feature on or off
LOCALITY To name a coded subspace
OPT_LEVELTo set the level of optimization
HP_SHLIB_VERSIONTo create a version of a shared library routine
VERSION IDTo specify a version string
ONCESpecify that file opened only once

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.


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.

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
#pragma OPT_LEVEL 2
// Optimize Func2() to at level 2
void Func2()

pragma OPTIMIZE:

The syntax of using the pragma OPTIMIZE is:


  • 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:


// Set optimization to level 2
// for Prog.C
aCC + O2 Prog.C
      void Func1
    // Turn off optimization for
    // this function
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[“]


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

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”


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

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. 


#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


  • 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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :