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
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. You can include the pragma directive in your C program from the point where you want
them 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. The syntax of
using a pragma directive can be given as
#pragma
string
Here,
string can be one of the instructions
given to the compiler with any required parameters. Table 10.1 describes some
pragma directives.
Pragma Copyright
The
syntax of pragma copyright can be given as
#pragma COPYRIGHT
"string"
Here,
string specifies the set of
characters included in the copyright message in the object file.
If
no date is specified using pragma COPYRIGHT_DATE,
then the current year is used in the copyright message. For example, if we
write
#pragma COPYRIGHT "JRT
Software Ltd"
Then
the following string is placed in the object code (assuming the current year is
2011):
Copyright
JRT Software Ltd, 2011. All rights reserved. No part of this program may be
photocopied, reproduced, or transmitted without prior written consent of JRT
Software Ltd.
Pragma
Copyright_date
The
syntax of pragma COPYRIGHT_DATE can
be given as
#pragma COPYRIGHT_DATE
"string"
Here,
the string is a date which will be
used by the COPYRIGHT pragma.
For
example, consider the pragma given below
#pragma COPYRIGHT DATE "1999-2011"
#pragma COPYRIGHT "JRT
Software Ltd."
The
above pragma will place the following string in the object code:
©
Copyright JRT Software Ltd, 1999-2011. All rights reserved. No part of this
program may be photocopied, reproduced, or transmitted without prior written
consent of JRT Software Ltd.
Pragma Optimize
The
syntax of using the pragma optimize can be given as
#pragma OPTIMIZE ON
#pragma OPTIMIZE OFF
The
pragma optimize is basically used to
turn on/off optimization in sections of a source program. However, when using
this pragma you must specify 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.
For example,
aCC +02 Prog.C // Set optimization
to level 2 for Prog.C
#pragma OPTIMIZE OFF
void Func1 ()
{ // Turn off optimization for this
function
………
}
#pragma OPTIMIZE ON
void Func2 ()
{ // Restore optimization to level
2
…………
}
Pragma OPT_LEVEL
The
syntax for pragma OPT_LEVEL which is
used to set the optimization level to 1,2,3, or 4 can be given as
#pragma OPT_LEVEL 1
#pragma OPT_LEVEL 2
#pragma OPT_LEVEL 3
#pragma OPT_LEVEL 4
Like
the optimization pragma, even this pragma
cannot be used within a function. Finally, OPT_LEVEL
3 and 4 are allowed only at the beginning of a file.
For
example,
aCC -O prog.C
#pragma OPT_LEVEL 1
void Func1 ()
{ // Optimize Funcl() at level 1.
………..
}
#pragma OPT_LEVEL 2
void Func2()
{ // Optimize Func2() at level 2.
………….
}
Note
The
kind of optimization done by the operating system at each level is beyond the
scope of this book.
Pragma
HP_SHLIB_VERSION
The
syntax for HP_SHLIB_VERSION pragma
which is used to create different versions of a shared library routine can be
given as
#pragma HP_SHLIB_VERSION
["]date ["]
Here,
the date 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 (99 for 1999) or a full year specification (1999). Here the two-digit year
codes from 00 to 40 are used to represent the years from 2000 to 2040,
respectively.
Note
The
version number applies to all global symbols defined in the module's source
file.
This
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 a name to be associated with
the code that is written to a re-locatable object module can be given as
#pragma LOCALITY "string"
Here, string specifies a name to be used for
a code subspace. After this directive, all codes following the directive are
associated with the name specified in string. The smallest scope of a unique LOCALITY pragma is a function.
Pragma VERSIONID
The
syntax of pragma VERSIONID can be
given as
#pragma VERSIONID "string"
Here,
string is a string of characters that
is placed in the object file. For example, if we write
#pragma VERSIONID "JRT
Software Ltd.,
Version 12345.A.01.10"
Then
this pragma places the characters JRT Software Ltd., Version 12345.A.01.10 into
the object file.
Pragma once
The
pragma once specifies that the file, in which this pragma directive is
specified will be included (opened) only once by the compiler in a building of
a particular file. Its 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 functions receive
parameters. In such cases, #pragma directive
provides machine- and operating-system-specific features for each compiler
while retaining overall compatibility with the C language.
Programming in C: Unit I (d): Preprocessor Directives : Tag: : with Example C Programs | Preprocessor Directives - Pragma Directives
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation