totn C Language

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:

DirectiveDescriptionExample
#includeInclude another C file into the current file at the location of the #include statement prior to compiling the source code.#include <stdio.h>
#defineDefine a macro which can be used as a constant throughout the source code.#define AGE 50
#undefClear a macro which was previously defined.#undef AGE
#ifConditional expresssion which can be used to include source code for compilation.#if AGE > 50
#ifdefAllows the inclusion of source code if the provided macro identifier has been defined. Equivalent to #if defined(identifier).#ifdef SOLARIS
#ifndefAllows the inclusion of source code if the provided macro identifier has not been defined.#ifndef WINDOWS
#elifProvides 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
#elseAllows the inclusion of source code if the preceeding #if, #ifdef, or #ifndef directive expression evaluates to false.#else
#endifSignals the end of a #if, #ifdef or #ifndef condition .#endif
#warningReport a warning message and continue preprocessing.#warning Non-critical error found
#errorReport error and stop preprocessing.#error Windows is an unsupported platform

Now let's take a moment to explain some of these preprocessor directives.