A statement written in the source code of a program that lets the programmer instruct the compiler to perform a specific operation within the compilation phase. For example, the #include
directive incorporates the contents of a header file to the compilation, which can add a little or a lot of code. The #define
directive is used to define a constant, for example #define LINEFEED '\x0A'
lets the programmer use a more friendly LINEFEED mnemonic instead of the hexadecimal \x0A notation. The #define can also be used for a condition. For example, the statement #define DEBUG
allows the programmer to execute code following #if (DEBUG)
to display results used only while testing.
A compiler directive is often required when the same source code is compiled on two different hardware platforms. For example, the code might be compiled and tested on Windows but compiled and run on Linux. In such cases, a few commands in the code may be different, and the #define
directives accomplish this.
In some languages, such as C/C++, compiler directives as in the above examples are called preprocessor directives because they are handled before the actual compilation process.
Pragmas are directives that are predefined and specific to the compiler or a group of compilers. For example, #pragma inline
means that assembly code is included within the source code. See compiler