The Proper Use of #include

You’re learning the ‘C’ language and what is the first program you write? “Hello World!” In that exercise, the first thing you do, besides opening some sort of text editor to create a text file, is use a “#include”. But what does that mean? Well, you may have a discussion with your Computer Science 101 teacher and classmates, or you may be learning on your own, ultimately you find out is that’s what we use to get access to other things in the language that we don’t write.

Most of us, at that point, don’t understand the nitty gritty details of what “#include” actually is. It is a preprocessor directive that literally takes that file and pastes the content of the “included” file there. Of course as an intro CS teacher, you don’t want to go down the rabbit hole of explaining there are actually several steps in the compilation chain for any programming language. You, know, you have to leave something for your other colleagues to teach in higher level classes.

The standard “Hello World!” exercise generally starts out with something like this:

#include <stdio.h>

Now there’s one huge thing here that we need to pay attention to that I believe we often gloss over when learning programming in “C/C++” languages. We’ve now introduced something amazing, “<>”. We added the little carrot brackets! Hopefully you’ll understand why this is so amazing by the end of this.

So at the end of the “Hello World!” exercise we end up with something that looks like this:

#include <stdio.h>

int main()
{
    printf("Hello World!\n");
    return 0;
}

An exercise we do shortly following the “Hello World!” is we make our own operations. In this exercise we implement our own header files. First we define our operation and put a stub of it in the header file. Take for example, in my “firstheader.h”:

int myOp(int arg);

In the same directory we create the implementation file called “firstimpl.c”. That contains:

int myOp(int arg)
{
    return arg;
}

We now modify our “helloworld.c” implementation file to utilize our custom operation.

#include <stdio.h>
#include "firstheader.h"

int main()
{
    printf("Hello World!:  %d\n", myOp(99));
    return 0;
}

Wait a minute! What is this? To include our own header we use the quotation marks? Now, this is what is amazing, the vast majority of programmers I’ve worked with, and even utilizing 3rd party software, have seemed to had this misconception burned into their brains. “To include our own header we use the quotation marks?”

Like most things in software, programming, heck science in general, the answer to that is “it depends!” In this, very basic case that is absolutely true, but when we get into more sophisticated system implementations, we need to be far more aware of the true meaning between “<>” and “”””. When we start adding files and start having to worry about software organization we need to fall back on the definition of what the difference is between the notations. Otherwise we are unable to utilize our toolsets properly which can lead to some expensive problems as the software base grows.

Now, realize, this semi-misconception just was burned into our brains by the most basic of programming exercises and it seems as though a huge amount of “C/C++” programmers don’t think about it beyond that. The true meaning and usage of the preprocessor directive can be very powerful in helping put together larger projects though.

The real meaning of “<>” is, search the search path first for the file (Note: you can call the included file anything you want, like “dope.dumb” and the preprocessor will insert it). You just have to be aware of whatever is included must be understandable to the language compiler itself). The real meaning of “””” is, search the current directory first. If not found search the search path.

Now that we know the defined meaning of the different notations for the “#include” directive, we must use it’s behavior to more appropriately set up our code organization and project compilation environments. More importantly after setting up the directory structures and build toolset, we need to use the correct notation.

Do not use “<>” if the included file is in the same directory. Do not use “””” if the included file is not in the same directory. If your coding standard says otherwise, fix the coding standard.

Peace! And Happy Coding!