C Language: Preprocessor Directives
This C tutorial explains how to use preprocessor directives in the C language.
Description
The preprocessor will process directives that are inserted into the C source code. These directives allow additional actions to be taken on the C source code before it is compiled into object code. Directives are not part of the C language itself.
Preprocessor directives begin with a pound (#) symbol and may have several arguments.
The following are some of the preprocessor directives that you can use in your source code:
Directive | Description | Example |
---|---|---|
#include | Include another C file into the current file at the location of the #include statement prior to compiling the source code. | #include <stdio.h> |
#define | Define a macro which can be used as a constant throughout the source code. | #define AGE 50 |
#undef | Clear a macro which was previously defined. | #undef AGE |
#if | Conditional expresssion which can be used to include source code for compilation. | #if AGE > 50 |
#ifdef | Allows the inclusion of source code if the provided macro identifier has been defined. Equivalent to #if defined(identifier). | #ifdef SOLARIS |
#ifndef | Allows the inclusion of source code if the provided macro identifier has not been defined. | #ifndef WINDOWS |
#elif | Provides an alternate inclusion of source code when used with the #if, #ifdef, or #ifndef directives and the #elif condition evaluates to true. | #elif YEARS_OLD > 10 |
#else | Allows the inclusion of source code if the preceeding #if, #ifdef, or #ifndef directive expression evaluates to false. | #else |
#endif | Signals the end of a #if, #ifdef or #ifndef condition . | #endif |
#warning | Report a warning message and continue preprocessing. | #warning Non-critical error found |
#error | Report error and stop preprocessing. | #error Windows is an unsupported platform |
Now let's take a moment to explain some of these preprocessor directives.
Advertisements