Directive (programming): Difference between revisions

Content deleted Content added
m Robot-assisted disambiguation (you can help!): C programming language
Merged from Compiler directive
Line 1:
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.
{{Merge|Preprocessor|date=November 2006}}
 
==Assembly language==
A '''directive''' is an instruction to a programming language compiler about how to compile a program. Rather than providing code per se, a programmer uses a directive to modify how or even if code is compiled.
In [[assembly language]], directives generally tell the assembler the target platform, mark the separations between [[section]]s, and so on. The mnemonic "ALIGN", which inserts in the current section as many [[byte]]s 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==
==Overview==
{{main|C preprocessor}}
Directives are used in several relatively low-level languages such as [[assembly programming]], [[C (programming language)|C]] and [[C++]]. In [[assembly language]], directives generally tell the assembler the target platform, delineate segments, and so on. In [[C++]], directives are used for conditional compiling, macros and including other files.
{{main|Preprocessor directive}}
 
In [[C (programming language)|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 <code>#define</code> and <code>#include</code>, are referred to as ''[[preprocessor directive]]s''.
==Examples==
===C/C++===
There are several preprocessor directives in [[C (programming language)|C]] and [[C++]]. The following are a few examples, but do not illustrate every directive or every situation in which one might find them. Note the lack of semicolons at the end of the lines: directives are instructions to the preprocessor, not the compiler, and they follow their own language grammar and rules.
 
One notable C preprocessor directive is <code>[[pragma once|#pragma once]]</code>, 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 guard|#include guards]].
#include <iostream>
 
Syntactic constructs similar to C's preprocessor directives, such as [[C Sharp|C#]]'s <code>#region</code>, are also typically called "directives", although in these cases there may not be any real preprocessing phase involved.
This is the most common form for most programmers. It instructs the preprocessor to read a file from disk and insert its contents into the source code file. In this case it reads the general I/O streams header from the compiler's standard header directory.
 
===C/C++See also===
#define PI (3.14159)
*[[Include guard]]
cout << (2.0 * PI) << endl;
 
==In other high-level languages==
Macros are, essentially, preprocessor variables that are expanded inline into the source code. This example defines a macro named PI that is expanded to 3.14159 everywhere it is found in the code. This is deprecated in most cases, as C and C++ both provide constants that are more efficient and provide strong typing.
In [[Ada (programming language)|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 [[comment (computer programming)|comment]]s. 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 <code>#include "file"</code> directive is the significant comment <code>{$I "file"}</code>.
#if defined LINUX
 
// Do some Linux stuff here
In [[Perl]], the construct <code>use strict;</code> is referred to as a "directive".{{fact}}
#elif defined WINDOWS
 
// Do some Windows stuff here
[[de:Compiler-Anweisung]]
#endif
[[es:Pragma]]
[[zh:編譯器指導指令]]
 
Another common directive, this generally is used for conditional compilation. This example shows a directive that chooses two sets of code to include in the preprocessor output depending on which operating system macro is defined.
 
[[Category:Computer programming]]