DEV Community

Cover image for The #include <> preprocessor statement.
Andreas Pappas
Andreas Pappas

Posted on

The #include <> preprocessor statement.

Hi there πŸ‘‹

The first sentence that I want to write here is the following,

The #include preprocessor statement, all that it does is literally copy and paste the contents of the header file specified after the #include <> into our file, on the particular line of our file that we placed that statement.

literally_amazed

πŸ™ƒ Alright then, keep reading and you might do so!

Remember my first sentence that the preprocessor statement #include all that does is take the file that is specified after it and literally copy paste it's content into our .cpp file?

Well let's prove that.

Let's say we have the following C++ code,

File: addNumbers.cpp

int addNumber(int x, int y) 
{
    return x + y;
}
Enter fullscreen mode Exit fullscreen mode

Simply we wrote a function that takes as input 2 integers and returns their sum.

If we try to compile the above code, it should compile successfully without any errors.

Now let's say that we forget the closing brace at the end of our code, so it looks like this:

int addNumber(int x, int y) 
{
    return x + y;
                        /// <--- notice the closing curly brace missing on this line.
Enter fullscreen mode Exit fullscreen mode

If we compile the below code we should get an error that we do miss a closing curly brace on line 4.

How can we fix that?

Either we can simply edit the file and add back our closing curly brace, or we can get a bit crazy and prove our very first statement of the #include preprocessor statement literally copy pasting into our addNumbers.cpp file the content of the header file that is after our include statement.

So let's create a header file.

File: closingBrace.h

}
Enter fullscreen mode Exit fullscreen mode

Yeah, exactly, our closingBrace.h file simply contains one character. The closing curly brace itself --> }

Now take a look at the below code,

int addNumber(int x, int y) 
{
    return x + y;
#include "closingBrace.h"
Enter fullscreen mode Exit fullscreen mode

What do you think will happen if we compile this file?

Well, it will compile properly without any errors.

Why is that?

Well, if you create the 2 files above in Microsoft Visual Studio and compile them, you can then see in your project a file with the extension .i in particular you will see a file called: closingBrace.i

What this file is, is simply a file that is generated by the Microsoft Visual Studio compiler and it simply contains the contents of the output of the preprocessor instead of directly creating the object file from our C++ source file.

How to generate the .i preprocessed file?


To generate a preprocessed file (.i file), just add "-EP -P" to the compile option, and recompile the original .cpp file. You can also generate a .i file directly from the "Prepocessor" property in Visual Studio*:

  1. Right click the file name, and click properties
  2. Click Preprocessor\Generate Preprocessed File\Without Line Numbers (/EP /P)

If you recompile from a command window, the .i file will be created under the current directory. if you recompile from within the Visual Studio* IDE, the .i file will be created under the directory where the original source file is located.


Well if you go on and open that file you will see the following contents:

see

int addNumber(int x, int y) 
{
    return x + y;
}
Enter fullscreen mode Exit fullscreen mode

Does it remind you of anything?

Well that was the proof that the preprocessor translated our #include "closingBrace.h" statement into a simple } character. And that is because it literally copy pasted the contents of the closingBrance.h file and replaced our #include statement with that content, and since we simply had only a } inside that file, that's what it copy pasted on that particular line that we placed our statement.

And that's all there is to it about the #include preprocessor statement and also about how we do use header files aka .h files in C++!


I hope you enjoyed the article and learned something new today as I myself did!

I've also started writing down a C++ course on my GitHub account on the following πŸ‘‰πŸ» repository, so you can check it out if you want to learn more cool stuff about how C++, it's compiler and linker do work and also best practices about writing fast and efficient C++ code.

I will be doing blog posts on several parts of the above course like this one in general, but if you want the A-Z of it then you can check the above repository mentioned!

Obviously contribution and corrections are much more than welcome!

You can also check if you wish so my recently made πŸ“– Blog website for more computer science but not limited to, related content and keep an eye πŸ‘€ on it as soon, I will make it's repository public with in depth detail on how you can also use that particular Blog website to customize it and use it as a template to make it your own if you like it and wish to do so❗


Thank you. πŸ™

©️ 2021 <Andreas><\Pappas>

Discussion (0)