|< C File I/O 5 | Main | C & C++ Preprocessor Directives 2 >|Site Index |Download |














My Training Period:        hours


The C/C++ preprocessor directives programming skills that should be acquired:





10.1    Introduction

  • For C/C++ preprocessor, preprocessing occurs before a program is compiled.  A complete process involved during the preprocessing, compiling and linking can be read inC/C++ - Compile, link and running programs.

  • Some possible actions are:

         Inclusion of other files in the file being compiled.

         Definition of symbolic constants and macros.

         Conditional compilation of program code or code segment.

         Conditional execution of preprocessor directives.

  • All preprocessor directives begin with #, and only white space characters may appear before a preprocessor directive on a line.

10.2    The #include Preprocessor Directive

  • The#include directive causes copy of a specified file to be included in place of the directive.  The two forms of the #include directives are:

// searches for header files and replaces this directive

// with the entire contents of the header file here

#include <header_file>

  • Or

#include "header_file"


e.g.         #include <stdio.h>

    #include "myheader.h"

  • If the file name is enclosed in double quotes, the preprocessor searches in the same directory (local) as the source file being compiled for the file to be included, if not found then looks in the subdirectory associated with standard header files as specified using angle bracket.

  • This method is normally used to include user or programmer-defined header files.

  • If the file name is enclosed in angle brackets (< and >), it is used for standard library header files, the search is performed in an implementation dependent manner, normally through designated directories such as C:\BC5\INCLUDE for Borland C++ (default installation) or directories set in the project options (compilers/IDEs) configuration.  You have to check your compiler documentation.  Compilers normally put the standard header files under the INCLUDE or INC directory.

The include directory paths set by default in the Visual C++ 2005 Express Edition Options page

  • The#include directive is normally used to include standard library such as stdio.h andiostream or user defined header files.  It also used with programs consisting of several source files that are to be compiled together. These files should have common declaration, such as functions, classes etc, that many different source files depend on those common declarations.

The header files seen under the default include folder of the Visual Studio .NET 2005

#include "/usr/local/include/test.h"

#include "sys/test1.h"

10.3    The #define Preprocessor Directive: Symbolic Constants

#define   identifier   replacement-text

#define  PI   3.14159

#defineSTR  "This is a simple string"

#defineNIL  ""

#defineGETSTDLIB  #include <stdlib.h>

#defineHEADER "myheader.h"


10.4    The #define Preprocessor Directive: Macros

#define    CIR_AREA(x)    PI*(x)*(x)

area = CIR_AREA(4);

area = 3.14159*(4)*(4);

area = CIR_AREA(y + 2);

area = 3.14159*(y + 2)*(y + 2);

area = 3.14159*y+2*y+2;

area = (3.14159 * y) + (2 * y) + 2;

double circleArea(double x)


       return (3.14159*x*x);


inline double circleArea(double x)


       return (3.14159 * x * x);


#define    RECTANGLE_AREA(p, q)    (p)*(q)

rectArea = RECTANGLE_AREA(a+4, b+7);

rectArea = (a+4)*(b+7);

#define    RECTANGLE_AREA(p, q)  \


#define    getchar()    getc(stdin)

#include <iostream>

using namespace std;

#define    THREETIMES(x)    (x)*(x)*(x)

#define    CIRAREA(y)    (PI)*(y)*(y)

#define    REC(z, a)    (z)*(a)

#define    PI    3.14159


int main(void)


    float p = 2.5;

    float r = 3.5, s, t, u = 1.5, v = 2.5;


    cout<<"Power to three of "<<p<<" is "<<THREETIMES(p)<<endl;

    cout<<"Circle circumference = 2*PI*r = "<<(2*PI*r)<<endl;


    s = CIRAREA(r+p);

    cout<<"Circle area = PI*r*r = "<<s<<endl;


    t = REC(u, v);

    cout<<"Rectangle area = u*v = "<<t<<endl;

    return 0;
















C C++ preprocessor directive program example


10.5    Conditional Compilation

#if   !defined(NULL)

     #define NULL 0


#if  0

   code prevented  from  compiling...


#ifdef DEBUG

   printf("Variable x = %d\n", x);


#define Module10

#define MyVersion 1.1

#include <iostream>

using namespace std;


int main(void)


    cout<<"Sample using #define, #ifdef, #ifndef\n";

    cout<<"  #undef, #else and #endif...\n";


    #ifdef Module10

       cout<<"\nModule10 is defined.\n";


       cout<<"\nModule10 is not defined.\n";



    #ifndef MyVersion

       cout<<"\nMyVersion is not defined\n";


       cout<<"\nMyVersion is "<<MyVersion<<endl;



    #ifdef MyRevision

       cout<<"\nMy Revision is defined\n"<<endl;


       cout<<"\nMyRevision is not defined!\n"<<endl;



    #undef MyVersion

    #ifndef MyVersion

       cout<<"MyVersion is not defined\n"<<endl;


       cout<<"MyVersion is "<<MyVersion<<endl;


    return 0;





C C++ preprocessor directive program example #if #ifndef #ifdef


  • If you check the header file definition, the conditional compilation directives heavily used as guard macro to guard the header files against multiple inclusion or filename redefinition.

  • For example, create the following header file namedboolean.h and save it in the same folder as your main() source file that follows.  Do not compile and run.

// testing the boolean.h header file from

// multiple inclusion or re-definition

#ifndef BOOLEAN_H

typedef int boolean;  // means literal string 'int' is same as 'boolean'


const boolean FALSE = 0;

const boolean TRUE =1;


#define BOOLEAN_H


  • This file defines the type and constants for the boolean logic if boolean.h has not been defined.

  • Then create the following program, compile and run.

// program using the user defined header file, boolean.h

#include <iostream>

      using namespace std;

// notice this...

#include "boolean.h"


int main(void)


   // new type stored in boolean.h...

  boolean    HappyTime;


   HappyTime = TRUE;


   // if TRUE = 1, do...


          cout<<"I'm happy today lor!!!"<<endl;

   // else, FALSE = 0, do...


          cout<<"What a bad day...today!!!"<<endl;

   return 0;





C C++ preprocessor directive program example output snapshot

#ifndef VEHICLE_H

#define VEHICLE_H

// The file is compiled only when VEHICLE_H is not defined.

// The first time the file is included using #include <vehicle.h>,

// the macro is not defined, then it is immediately defined.

// Next time, if the same inclusion of the vehicle.h or

// by other source file with the same vehicle.h, needs to be

// included in the same project, the macro and conditional directives

// ensure that the file inclusion is skipped…


class vehicle{...};



// end of the vehicle.h

C & C++ programming tutorials








Further related preprocessor directives reading and digging:


  1. A complete process involved during the preprocessing, compiling and linking can be read inC/C++ - Compile, link and running programs.
  2. Check the best selling C / C++ books at Amazon.com.






|< C File I/O 5 | Main | C & C++ Preprocessor Directives 2 >|Site Index |Download |

C & C++ Preprocessor Directives: Part 1 | Part 2 |