Directive (programming)

This is an old revision of this page, as edited by Quuxplusone (talk | contribs) at 04:18, 1 June 2007 (Merged from Compiler directive). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer programming, the word directive may refer to any of several related concepts. Generally, programming languages distinguish between source code instructions, which are translated into output such as machine code or data; and directives, which do not correspond to any particular part of the output, but simply tell the compiler or assembler how to treat the input.

Assembly language

In assembly language, directives generally tell the assembler the target platform, mark the separations between sections, and so on. The mnemonic "ALIGN", which inserts in the current section as many bytes as needed to preserve word-alignment, is also generally referred to as a "directive", despite the fact that it does correspond to a particular construct in the generated code.

The C preprocessor

In C and C++, the language provides a simple macro preprocessor that essentially makes a pass over the source code before the traditional lexing and parsing of the compiler proper. (See C preprocessor.) Source lines that are intended for the preprocessor, such as #define and #include, are referred to as preprocessor directives.

One notable C preprocessor directive is #pragma once, which instructs the compiler to include the current source file only once, making it effectively idempotent, and providing an alternative in C and C++ to #include guards.

Syntactic constructs similar to C's preprocessor directives, such as C#'s #region, are also typically called "directives", although in these cases there may not be any real preprocessing phase involved.

See also

In other high-level languages

In Ada, compiler directives are called pragmas (short for "pragmatic information").

In Turbo Pascal, directives are called significant comments, because in the language grammar 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 {$I "file"}.

In Perl, the construct use strict; is referred to as a "directive".[citation needed]