Preprocessor Directives C Tutorial

Bodhak C preprocessor directives c tutorial in this chapter we will learn about Introduction to Preprocessor Directives, Preprocessor directive categories, Macro substitution directives, File inclusion directives, Compiler control directives.

C Preprocessor: C is a unique language in many respects. We have already seen features such as functions and arrays. Yet another unique feature of  “C” language is the preprocessor.



preprocessor directives c tutorial

The C preprocessor provides several tools that are unavailable in other high-level languages. The programmer can use these tools to make his program easy to read, easy to modify, portable, and more efficient.

The preprocessor, as its name implies, is a program that processes the source code before it passes through the compiler. It operates under the control of what is known as preprocessor command lines or directives. Preprocessor directives are placed in the source program before the main line. Before the source code passes through the compiler, the preprocessor for any preprocessor directives examines it. If there are any, appropriate actions (as per the directives) are taken and then the source program is handed over to the compiler.

Preprocessor directives follow special syntax rules that are different from the normal C syntax. They all begin with the symbol # in column one and do not require a semicolon at the end. We have already used the directives #define and #include to a limited extent.

Preprocessor Directives

Directive  Function


#define  Defines a macro substitution

#undef       Undefines a macro

#include        Specifies the files to be included

#ifdef        Test for a macro definition

#endif        Specifies the end of #if.

#ifndef    Tests whether a macro is not defined

#if        Test a compile-time condition

#else        Specifies alternatives when #if test fails


These directives can be divided into three categories:

Macro substitution directives.

File inclusion directives.

Compiler control directives.

Macro Substitution:

Macro substitution is a process where an identifier in a program is replaced by a predefined string composed of one or more tokens. The preprocessor accomplishes this task under the direction of  #define statement. This statement usually known as a macro definition (or simply a macro) takes the following general form:


#define identifier string

If this statement is included in the program at the beginning, then the preprocessor replaces every occurrence of the identifier in the source code by the string, with at least one blank space between them. Note that the definition is not terminated by a semicolon. The string may be any text, while the identifier must be a valid C name.

There are different forms of macro substitution. The most common forms are:

Simple macro substitution.

Argumented macro substitution.

Nested macro substitutions