C++ the #ifndef , #define preprocessor directives

If you look at any header’s file (.h file) of a library,before any real code is written you will always find the preprocessor directives #ifndef followed by #define and at the end of the code #endif is added.

The directive #ifndef means:if not defined,and it’s task is to check if a macro has been defined or exist.If the macro does not exist then the next line is executed and if the macro exist nothing is done.

These directive and the other directives comes in handy while writing a header’s file,but how? Consider a header’s file name limits.h in this file the directives are written in the form as shown below.

#ifndef _LIMITS_H_
#define _LIMITS_H_

//your code here

#endif   /* _LIMITS_H_ */ 

The directives #ifndef plays a very important role in a header’s file.It tries to prevent multiple inclusion of the content of the file whenever the compiler finds the statement #include<limits.h> multiple times in multiple files.

Consider that you have included <limits.h> in your main.cpp file,when compiling the file if the compiler come across the directives ‘#ifndef _LIMITS_H_’ for the first time it will check if the macro _LIMITS_H_ exists.Since the macro does not exist(since it is first time the file is included) the next statement “#define _LIMITS_H_” will define the macro _LIMITS_H_ and the content found in the remaining parts of the file is added to your main.cpp file.

Suppose another library file say ‘my_lib.h’ also include the file <limits.h>.When the compiler include the file ‘my_lib.h’ it will also try to include the file ‘limits.h’ again because the statement ”#include <limits.h>” was also found in the ‘my_lib.h’ file.This time what happen is when the compiler tries to include the file limits.h it come across the directive #ifndef _LIMITS_H_ but since _LIMITS_H_ had already been defined when the main.cpp was processed,the statement #ifndef _LIMITS_H_ yields false.Now,the compiler has no choice but to not include the file.Hence multiple inclusion of the file is prevented.

If the directives were not written then the compiler will try to include the library file whenever it come across “#include <limits.h>” in multiple file and this will give you an error.To illustrate the case when an error occurs consider the two library files written without the directives.The first file is header1.h and it’s content is shown below.

/*header1.h file */
//#ifndef , #define are not included

#include “header2.h”

typedef long long LL ;

The second library header’s file is header2.h.

/*header2.h file */

#include “header1.h”

LL increment(LL val)   //LL is from header1 typedef
{
return ++val ;
}

The last file is the main.cpp file.

#include <iostream>
#include “header2.h”

using namespace std;

int main( )
{
cout<< increment(90 ) ;//call the function in header2.h 

cin.get() ;
return 0 ;
}

Try running the program with the library files content as shown above undoubtedly you will get an error.

In the main.cpp, header2.h is included so it’s content is loaded by the compiler and also you can see that header2.h file include the header’s file header1.h hence header1.h file content is also loaded.


Inside header1.h the header2.h file is included so the compiler will try to load again header2.h file content but to no avail cause it’s content is already added.To this attempt in trying to include the content of header2.h multiple time the compiler will generate an error.The conclusion that can be drawn is never forget to write the directives #ifndef ,#define and #endif in your header’s file.



Leave a Reply

Your email address will not be published. Required fields are marked *