C++ Preprocessor directives #include

In C++ preprocessor directives are keywords with the character ‘#’ attached in-front.There are many preprocessor directives with varied function and uses,in this post we shall discuss the preprocessor directives ‘#include‘.

#include

When you wrote your first program I am sure you have come across the preprocessor directives ‘#include’ or just to remind you,you might have come across the line.

#include <iostream> 
//'#include' directives written before <iostream>

The above line is usually interpreted as including the header’s file content(header’s file refer to any file enclosed between < >(angle bracket) or ” “(double quotation)).When the compiler come across this directives and the file name(‘iostream’ in this case) it will try to include the member functions or any other directives (like typdef,#define …) or ‘const’ found inside the file iostream in your program.By including those we will be able to use them in our program later on.For instance,without including the iostream file we cannot use ‘cout’ in our program because ‘cout’ is part of the ‘iostream’ file.

So summing up “#include does nothing but simply command the compiler to add all the content found inside the header’s file to the main source file so that we can utilize it in our program.


Difference between writing header’s file under “”(double quotation) and angle bracket(<>)

The header’s file is usually written inside the opening and closing angle bracket(< >) but sometimes we are compel to write the header’s name under “”(double quotation).The reason behind this difference is quite simple.

When you make a new library of your own say “mylib.h” and “mylib.cpp”,you might keep these two files in the directory where your main.cpp file is present.I usually keep my program in the directory “c:/my_program/”,suppose, if I am making a program (name first_program) the main.cpp will be present in the directory “c:/my_program/first_program” and the library file “mylib.h” and “mylib.cpp” will be also found in that directory(assuming they are kept in that directory).

To call the function present in ‘mylib.h’ in my program I will have to write ‘#include “mylib.h”‘ not ‘#include <mylib.h>‘.Using the double quotation tell the compiler to search the file ‘mylib.h’ in the directory “c:/my_program/first_program” and not in the standard directory which is usually “C:/CodeBlocks/MinGW/include/” where all the standard C++ header’s file are present.

If you want to use the standard syntax(the angle bracket) to include the file ‘mylib.h’ then transfer the file to the directory “C:/CodeBlocks/MinGW/include/”,after doing so you can write ‘#include <mylib.h>’ and it will work.An illustrated program is shown below.

//mylib.h file found in “C:/CodeBlocks/MinGW/include/”

#ifndef MYLIB_H_INCLUDED
#define MYLIB_H_INCLUDED

#include <string>

using namespace std ;

string append(string str_first ,string str_second );

#endif // MYLIB_H_INCLUDED

Link: C++ #ifndef and #define directives

The next file is mylib.cpp file found in the same directory as the main.cpp.

/***The mylib.cpp file ***/
#include <mylib.h>

using namespace std;

string append(string str_first ,string str_second )
{
return str_first+str_second ;
} 

Lastly the main.cpp file.



#include <iostream>
#include <mylib.h> //work fine

using namespace std;

int main( )
{
string s1=”Uzumaki ” , s2=”Naruto”;
cout<< append(s1,s2) ;

return 0;
} 

The difference between ” “ and < > is that “” represent the current directory while the <> represent the standard directory.So when the compiler come across the double quotation or the opening and closing angel bracket(< >),it simply searches for the file in their concerned directories.

While writing ‘mylib.h’ some other preprocessor directives like #ifndef ,#define and #endif are used,their uses is discuss in another post C++ #ifndef,#define directives.



Leave a Reply

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