In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, a directive or pragma (from "pragmatic") is a
language construct that specifies how a
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
(or other
translator) should process its input. Depending on the
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
, directives may or may not be part of the
grammar
In linguistics, grammar is the set of rules for how a natural language is structured, as demonstrated by its speakers or writers. Grammar rules may concern the use of clauses, phrases, and words. The term may also refer to the study of such rul ...
of the language and may vary from compiler to compiler. They can be processed by a
preprocessor
In computer science, a preprocessor (or precompiler) is a Computer program, 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 i ...
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''.
The C preprocessor
In
C and
C++, the language supports a simple
macro preprocessor
In computer science, a preprocessor (or precompiler) is a Computer program, 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 i ...
. Source lines that should be handled by the preprocessor, such as
#define
and
#include
are referred to as ''preprocessor directives''.
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, except for
defined
(when following a conditional directive), begin with a hash symbol (#). Until
C++26, the keywords
export
,
import
and
module
were partially handled by the preprocessor.
History
Directives date to
JOVIAL.
COBOL has a COPY directive.
In
ALGOL 68
ALGOL 68 (short for ''Algorithmic Language 1968'') is an imperative programming language member of the ALGOL family that was conceived as a successor to the ALGOL 60 language, designed with the goal of a much wider scope of application and ...
, directives 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:
Today directives are best known in the C language, of early 1970s vintage, and continued through the current
C99 standard, where they are either instructions to the
C preprocessor, 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 American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
, 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, directives are called significant comments, because in the language
grammar
In linguistics, grammar is the set of rules for how a natural language is structured, as demonstrated by its speakers or writers. Grammar rules may concern the use of clauses, phrases, and words. The term may also refer to the study of such rul ...
they follow the same syntax as
comments. In Turbo Pascal, a significant comment is a comment whose first character is a
dollar sign 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 high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
, 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 pioneered several programming language ...
pragmas are specified using a specialized comment syntax, e.g.
. It is also possible to use the C preprocessor in Haskell, by writing .
* PHP uses the directive declare(strict_types=1)
.
* In PL/I
PL/I (Programming Language One, pronounced and sometimes written PL/1) is a procedural, imperative computer programming language initially developed by IBM. It is designed for scientific, engineering, business and system programming. It has b ...
, directives begin with a Percent sign
The percent sign (sometimes per cent sign in British English) is the symbol used to indicate a percentage, a number or ratio as a fraction (mathematics), fraction of 100. Related signs include the permille (per thousand) sign and the Basis p ...
(%
) and end with a semicolon (;
), e.g., %INCLUDE ''foo'';
, %NOPRINT;
, %PAGE;
, %POP;
, %SKIP;
, the same as with preprocessor statements.
* 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 standard for scripting languages, including JavaScript, JScript, and ActionScript. It is best known as a JavaScript standard intended to ensure the interoperability of web pages across different web browsers. It is stan ...
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), the current version of Visual Basic launched in 2002 which runs on .NET
* Visual Basic (classic), the original Visual Basic suppo ...
, 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
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 sapph ...
, 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 character encoding standard used for electronic communication. Defined by the Unicode Standard, the name is derived from ''Unicode Transformation Format 8-bit''. Almost every webpage is transmitted as UTF-8.
UTF-8 supports all 1,112,0 ...
character encoding
Character encoding is the process of assigning numbers to graphical character (computing), characters, especially the written characters of human language, allowing them to be stored, transmitted, and transformed using computers. The numerical v ...
.
* In C#, compiler directives are called pre-processing directives. C# does not technically handle these using a preprocessor, but rather directly in the code. There are a number of different compiler directives, which mostly align with those from C and C++, including #pragma
, which is specifically used to control compiler warnings and debugger checksums. C# also features some directives not used in C or C++, including #nullable
and #region
. C# also does not allow function-like macros, but does allow regular macros, for purposes such as conditional compilation.
* The SQLite DBMS
In computing, a database is an organized collection of data or a type of data store based on the use of a database management system (DBMS), the software that interacts with end users, applications, and the database itself to capture and ana ...
includes a PRAGMA directive that is used to introduce commands that are not compatible with other DBMS.
* In Solidity, compiler directives are called pragmas, and are specified using the `pragma` keyword.
Assembly language
* In assembly language
In computing, assembly language (alternatively assembler language or symbolic machine code), often referred to simply as assembly and commonly abbreviated as ASM or asm, is any low-level programming language with a very strong correspondence bet ...
, 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 corporation, multinational computer technology company headquartered in Austin, Texas. Co-founded in 1977 in Santa Clara, California, by Larry Ellison, who remains executive chairman, Oracle was ...
's PL/SQL 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