Why we use Header Files in C and C++

Devansh Gupta
4 min readDec 5, 2019

Last week I met my younger cousin brother after a long time.

He is in 11th Standard. We were talking about his studies. He told me about all the Subject he have in its 11th standard syllabus. He is also very curious about C language as they have a subject for same. He asked me a question that why every Program in C have something like,

C language Boilerplate code

He was specifically curious about the Header File stuff.

So In this story, We are going to discuss some basics of C Header file.

Why we need Header File

Modular Development of software is an common and essential practice. In simple words, To build large software systems we need a lots code. I mean a lots of code. (e.g. Linux Os is written in 12 Million lines of code)

This much amount of code, cannot reside in same file. It’s completely impractical and nearly impossible. To tackle this problem, We need some mechanism to divide our code ( Yes! I am talking about those 12 M lines of code). Here comes the Header file to rescue us.

So How things Work

First we write some code in a text file and saves it with .h extension

Header File “myheader.h”

And you may ask “Is it necessary to save the file with .h extension always ?” And the answer is “No”. You can save this file with any extension (e.g. .h, .hpp, .c, .cpp, .blahblah) . But conventionally we use .h in C and .hpp in C++ so that we can distinguish between normal source file and header files.

We are then free to use this header file wherever we need this.

myhedaer_user.c

Hey, did you notice that we include our custom header file with #include”myheader.h” and predefined header file stdio.h with #include<stdio.h>. Why did we do that ? To answer this question we need to know what happens when we compile our C/C++ source code.

  1. We first write our C/C++ code, and then saves this code in text file
  2. We fire our C compiler to compile the C code.
  3. Before compiling the code our compiler runs a program called PreProcessor. Compiler pre processes our source code, in this step it aggregates all the included header files into our source code. for example when We run preprocessor on following soruce code

We get the following output

4. Compiler then compiles this aggregated source code.

So to aggregate all the header files with our source code, Compiler first needs to find those header file. To find these files he checks default header file directory, for linux systems it is “/usr/include”. In above example our compiler is not going to find “myheader.h” because it’s not a standard c header file. To help compiler we use “myheader.h” instead of <myheader.h> which tells compiler that compiler should check for the header in same directory where the source file exists.

Thats how it works.

Resolving Header File Redundancy

Imagine a scenario,

We have 2 header files 1.h and 2.h

1.h
2.h which includes 1.h

We we have a program which uses these header file

Let’s watch its preprocessed version look like

And when we compile 3.c We got the following message

What the heck is going on?

Actually our source have 2 definition of foo(), and compiler is confused between both the definitions. This is a common problem with header file mechanism. The real problem here is that How we are going to handle multiple includes to the same header file. In this example 3.c includes 1.h and 2.h also includes 1.h , so 1.h is included 2 times in the final source code.

To handle this problem we can use macros,

At start of every file we set an macro and checks whether this macro exists previously or not . If macro is set before header file is not included else code is included

for example, Here is the update version of 1.h

Improved 1.h

Let me explain this Line by Line,

  1. We first check if 1_H macro constant is not defined or not, and if it not defined
  2. Second line is executed and a Macro constant 1_H is defined

3–6. Contains the code to be included

7. Closes our branching statement

And It resolves our problem of Header file Redundancy.

This was all I explained to my cousin. I know , its very long.

--

--