Directive (programming) explained

In computer programming, a directive or pragma (from "pragmatic") is a language construct that specifies how a compiler (or other translator) should process its input. Depending on the programming language, directives may or may not be part of the grammar of the language and may vary from compiler to compiler. They can be processed by a preprocessor 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

See main article: C preprocessor.

In C and C++, the language supports a simple macro preprocessor. 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 begin with a hash symbol (#) with the exception of the import and module directives in C++.[1]

History

Directives date to JOVIAL.[2]

COBOL Had a COPY directive.

In ALGOL 68, 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: .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 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

Assembly language

PL/SQL

References

External links

Notes and References

  1. Web site: P1857R1 - Modules Dependency Discovery.
  2. Computer Programming Manual for JOVIAL (J73) Language . RADC-TR-81-143 . June 1981 . Chapter 17 - Directives . https://apps.dtic.mil/sti/pdfs/ADA101061.pdf#page=248 . 243-263 . May 28, 2023 .
  3. Web site: 7.20. Pragmas. GHC 7.8.3 Documentation. 18 July 2014.
  4. Web site: Lexical structure - C# language specification. dotnet-bot. docs.microsoft.com. en-us. 2019-11-01.
  5. Web site:
    1. pragma - C# Reference
    . BillWagner. docs.microsoft.com. en-us. 2019-11-01.
  6. Web site: Pragma statements supported by SQLite . www.sqlite.org.
  7. Web site: Layout of a Solidity Source File — Solidity 0.8.27 documentation . 2024-06-03 . docs.soliditylang.org.
  8. Book: Feuerstein. Steven. Steven Feuerstein. Pribyl. Bill. Oracle PL/SQL Programming. 23 January 2014. 6. O'Reilly Media, Inc.. 2014. 9781449324414. 2016-06-16. PL/SQL has a PRAGMA keyword with the following syntax: PRAGMA instruction_to_compiler; [...] PL/SQL offers several pragmas [...].