In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, a directive or pragma (from "pragmatic") is a
language construct
In computer programming, a language construct is a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of the programming language.
The term "language construct" is often use ...
that specifies how a
compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
(or other
translator
Translation is the communication of the meaning of a source-language text by means of an equivalent target-language text. The English language draws a terminological distinction (which does not exist in every language) between ''transla ...
) should process its input. Directives are not part of the
grammar
In linguistics, the grammar of a natural language is its set of structure, structural constraints on speakers' or writers' composition of clause (linguistics), clauses, phrases, and words. The term can also refer to the study of such constraint ...
of a
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming l ...
, and may vary from compiler to compiler. They can be processed by a
preprocessor
In computer science, a preprocessor (or precompiler) is a program that processes its input data to produce output that is used as input in another program. The output is said to be a preprocessed form of the input data, which is often used by s ...
to specify compiler behavior, or function as a form of
in-band parameterization.
In some cases directives specify global behavior, while in other cases they only affect a local section, such as a block of programming code. In some cases, such as some C programs, directives are optional compiler hints, and may be ignored, but normally they are prescriptive, and must be followed. However, a directive does not perform any action in the language itself, but rather only a change in the behavior of the compiler.
This term could be used to refer to proprietary third party tags and commands (or markup) embedded in code that result in additional executable processing that extend the existing compiler, assembler and language constructs present in the development environment. The term "directive" is also applied in a variety of ways that are similar to the term ''command''."cc"
,c
"ccc",''
"cc"
The C preprocessor
In
C and
C++, the language supports a simple
macro
Macro (or MACRO) may refer to:
Science and technology
* Macroscopic, subjects visible to the eye
* Macro photography, a type of close-up photography
* Image macro, a picture with text superimposed
* Monopole, Astrophysics and Cosmic Ray Observat ...
preprocessor
In computer science, a preprocessor (or precompiler) is a program that processes its input data to produce output that is used as input in another program. The output is said to be a preprocessed form of the input data, which is often used by s ...
. Source lines that should be handled by the preprocessor, such as
#define
and
#include
are referred to as ''preprocessor directives''.
Another C construct, the
#pragma
directive, is used to instruct the compiler to use pragmatic or implementation-dependent features. Two notable users of this directive are
OpenMP
OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared-memory multiprocessing programming in C, C++, and Fortran, on many platforms, instruction-set architectures and operating sy ...
and
OpenACC
OpenACC (for ''open accelerators'') is a programming standard for parallel computing developed by Cray, CAPS, Nvidia and PGI. The standard is designed to simplify parallel programming of heterogeneous CPU/ GPU systems.
As in OpenMP, the program ...
.
Syntactic constructs similar to C's preprocessor directives, such as
C#'s
#if
, are also typically called "directives", although in these cases there may not be any real preprocessing phase involved.
All preprocessor commands begin with a hash symbol (#).
History
Directives date to
ALGOL 68
ALGOL 68 (short for ''Algorithmic Language 1968'') is an imperative programming language that was conceived as a successor to the ALGOL 60 programming language, designed with the goal of a much wider scope of application and more rigorously ...
, where they are known as
pragmats (from "pragmatic"), and denoted pragmat or pr; in newer languages, notably C, this has been abbreviated to "pragma" (no 't').
A common use of pragmats in ALGOL 68 is in specifying a
stropping regime, meaning "how keywords are indicated". Various such directives follow, specifying the POINT, UPPER, RES (reserved), or quote regimes. Note the use of stropping for the pragmat keyword itself (abbreviated pr), either in the POINT or quote regimes:
.PR POINT .PR
.PR UPPER .PR
.PR RES .PR
'pr' quote 'pr'
Today directives are best known in the C language, of early 1970s vintage, and continued through the current
C99
C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps imp ...
standard, where they are either instructions to the
C preprocessor
The C preprocessor is the macro preprocessor for the C, Objective-C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line contro ...
, or, in the form of
#pragma
, directives to the compiler itself. They are also used to some degree in more modern languages; see below.
Other languages
* In
Ada, compiler directives are called pragmas (short for "pragmatic information").
* In
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fr ...
, directives are called declarations, and are specified using the
declare
construct (also
proclaim
or
declaim
). With one exception, declarations are optional, and do not affect the semantics of the program. The one exception is
special
, which must be specified where appropriate.
* In
Turbo Pascal
Turbo Pascal is a software development system that includes a compiler and an integrated development environment (IDE) for the Pascal programming language running on CP/M, CP/M-86, and DOS. It was originally developed by Anders Hejlsberg at ...
, directives are called significant comments, because in the language
grammar
In linguistics, the grammar of a natural language is its set of structure, structural constraints on speakers' or writers' composition of clause (linguistics), clauses, phrases, and words. The term can also refer to the study of such constraint ...
they follow the same syntax as
comments. In Turbo Pascal, a significant comment is a comment whose first character is a
dollar sign
The dollar sign, also known as peso sign, is a symbol consisting of a capital " S" crossed with one or two vertical strokes ($ or ), used to indicate the unit of various currencies around the world, including most currencies denominated " ...
and whose second character is a letter; for example, the equivalent of C's
#include "file"
directive is the significant comment
.
* In
Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
, the
keyword "
use
/code>", which imports modules, can also be used to specify directives, such as use strict;
or use utf8;
.
* Haskell
Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
pragmas are specified using a specialized comment syntax, e.g.
.
* Python has two directives – from __future__ import feature
(defined i
PEP 236 -- Back to the __future__
, which changes language features (and uses the existing module import syntax, as in Perl), and the coding
directive (in a comment) to specify the encoding of a source code file (defined i
PEP 263 -- Defining Python Source Code Encodings
. A more general directive statement was proposed and rejected i
PEP 244 -- The `directive' statement
these all date to 2001.
* ECMAScript
ECMAScript (; ES) is a JavaScript standard intended to ensure the interoperability of web pages across different browsers. It is standardized by Ecma International in the documenECMA-262
ECMAScript is commonly used for client-side scriptin ...
also adopts the use
syntax for directives, with the difference that pragmas are declared as string literals (e.g. "use strict";
, or "use asm";
), rather than a function call.
*In Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to:
* Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET
* Visual Basic (c ...
, the keyword "Option
" is used for directives:
** Option Explicit On, Off
- When on disallows implicit declaration of variables at first use requiring explicit declaration beforehand.
** Option Compare Binary
- Results in string comparisons based on a sort order derived from the internal binary representations of the characters - e.g. for the English/European code page (ANSI 1252) A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø. Affects intrinsic operators (e.g. =, <>, <, >), the Select Case block, and VB runtime library string functions (e.g. InStr).
** Option Compare Text
- Results in string comparisons based on a case-insensitive text sort order determined by your system's locale - e.g. for the English/European code page (ANSI 1252) (A=a) < (À = à) < (B=b) < (E=e) < (Ê = ê) < (Z=z) < (Ø = ø). Affects intrinsic operators (e.g. =, <>, <, >), the Select Case block, and VB runtime library string functions (e.g. InStr).
** Option Strict On, Off
- When on disallows:
*** typeless programming - where declarations which lack an explicit type are implicitly typed as Object.
*** late-binding (i.e. dynamic dispatch to CLR, DLR, and COM objects) on values statically typed as Object.
*** implicit narrowing conversions - requiring all conversions to narrower types (e.g. from Long to Integer, Object to String, Control to TextBox) be explicit in code using conversion operators (e.g. CInt, DirectCast, CType).
** Option Infer On, Off
- When on enables the compiler to infer the type of local variables from their initializers.
* In Ruby
A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum (aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapp ...
, interpreter directives are referred to as pragmas and are specified by top-of-file comments that follow a key: value
notation. For example, coding: UTF-8
indicates that the file is encoded via the UTF-8
UTF-8 is a variable-length character encoding used for electronic communication. Defined by the Unicode Standard, the name is derived from ''Unicode'' (or ''Universal Coded Character Set'') ''Transformation Format 8-bit''.
UTF-8 is capable of ...
character encoding
Character encoding is the process of assigning numbers to graphical characters, especially the written characters of human language, allowing them to be stored, transmitted, and transformed using digital computers. The numerical values tha ...
.
*In C#, compiler directives are called pre-processing directives. There are a number of different compiler directives including #pragma, which is specifically used to control compiler warnings and debugger checksums.
* The SQLite
SQLite (, ) is a database engine written in the C programming language. It is not a standalone app; rather, it is a library that software developers embed in their apps. As such, it belongs to the family of embedded databases. It is the mo ...
DBMS
In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases span ...
includes a PRAGMA directive that is used to introduce commands that are not compatible with other DBMS.
* In Solidity
Solidity is an object-oriented programming language for implementing smart contracts on various blockchain platforms, most notably, Ethereum. It was developed by Christian Reitwiessner, Alex Beregszaszi, and several former Ethereum core contri ...
, compiler directives are called pragmas.
Assembly language
* In assembly language, directives, also referred to as pseudo-operations or "pseudo-ops", generally specify such information as the target machine, mark separations between code sections, define and change assembly-time variables, define macros, designate conditional and repeated code, define reserved memory areas, and so on. Some, but not all, assemblers use a specific syntax to differentiate pseudo-ops from instruction mnemonics, such as prefacing the pseudo-op with a period, such as the pseudo-op .END
, which might direct the assembler to stop assembling code.
PL/SQL
* Oracle Corporation
Oracle Corporation is an American multinational computer technology corporation headquartered in Austin, Texas. In 2020, Oracle was the third-largest software company in the world by revenue and market capitalization. The company sells da ...
's PL/SQL
PL/SQL (Procedural Language for SQL) is Oracle Corporation's procedural extension for SQL and the Oracle relational database. PL/SQL is available in Oracle Database (since version 6 - stored PL/SQL procedures/functions/packages/triggers since ...
procedural language includes a set of compiler directives, known as "pragmas".[
]
See also
*
Footnotes
References
*
External links
OpenMP Website
OpenACC Website
OpenHMPP Website
{{DEFAULTSORT:Directive (Programming)
Computer programming