Science Fair Project Encyclopedia
Directive (programming)
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.
Overview
Directives are used in several relatively low-level languages such as assembly programming, 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.
Examples
C/C++
There are several preprocessor directives in 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.
#include <iostream>
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.
#define PI (3.14159) cout << (2.0 * PI) << endl;
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.
#if defined LINUX // Do some Linux stuff here #elif defined WINDOWS // Do some Windows stuff here #endif
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.
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details


