Inbuilt and User defined Header Files in C Language

In the last tutorial Preprocessors in C Programming we saw a feature of Preprocessor known as file inclusion which allows us to include another file or header file into our program. In this tutorial, we will go through that process in detail and understand what are header files and how we make use of them in our program to remove complexities from our code.

Header Files in C

Header files are the files that contain macro definitions and comes with a .h extension. It is included in the user-written code to gain access to certain compiler functionalities and also other functionalities that the programmer defines. In simple words, we include another file into our code to use the constants and functions of that file. It is possible to not include any file and write the whole function down in the code. But that would make our code prone to errors and by making changes to only certain parts of the program makes it more susceptible to errors, but that is more like rewriting each and everything which is never beneficial and also breaks the DRY(Don’t repeat yourself) rule of good programming. In short, we never follow this practice and always use the features defined by the language.

There are two types of Header files in the C programming language:

  1. In-built Header files: The header files that our compiler provides us
  2. User defined Header files: The header files that we can create according to our need.

Syntax to include Header Files

Both the user defined and inbuilt header files in C can be included in the program using the #include preprocessor. Let’s have a look at the syntax that we use in order to include the file into our program:

  • #include<filename.h> : We use this syntax for the inbuilt header files or system header files. There is a list of header files in the system directory and the compiler imports the file from that location.
  • #include “filename.h” : (<> are replaced by “”)We use this syntax for the user defined header files that are stored in the directory by the user to be accessed by the compiler.

In-built header files

In-built header files are those which already exists in the compiler and all a user has to do is include it into the code to gain access to its functions, constants and other definitions contained within the header file. In-built header files contain specific definitions and declarations which allow us to build an interface between the code and the operating system and other system libraries. Let’s have a look at these In-built header files to have a better understanding:

  • <stdio.h>      : The very basic one which enables input and output.
  • <math.h>      : Allows us to use the Mathematical functions
  • <conio.h>      : Has certain in-built functions like clrscr(), getch(), etc.
  • <complex.h>: Complex number arithmetic
  • <float.h>        : Contains constants related to floating point values.
  • <tgmath.h>   : Combines math.h and complex.h
  • <time.h>         : time/date utilities
  • <stdbool.h>   : Boolean datatype
  • <signal.h>      : Signal handling

Here are a few(of many) inbuilt header files that are commonly used in the C programming Language.


User Defined Header Files

The name, user-defined header file is self-explanatory. C language offers a feature which enables us to create a header file and include it into our code. Let’s follow up the steps to creating our own header files in C. Here we use a simple function to find out the cube of a number.

Step 1: Use a text editor(Here Notepad++) to type in the following code(You can define any function of your choice).


int cube(int n)
{
    return n*n*n;
}


Step 2: Save this file with a .h extension(here cube.h) to the directory where other header files are stored. Inside the Compiler folder in the C:\ drive(here Cygnus) you will find the folder containing the header files(here include).

Step 3: Write a program as follows(or any other code according to your choice) and include the preprocessor as #include “cube.h”  in your program with the other headers. To use the function just type the functionname(parameter).[here cube(num)]


#include<stdio.h>
#include "cube.h"
int main()
{
    int num;
    printf("Enter a number:\n");
    scanf("%d",&num);
    printf("Cube of %d is %d",num,cube(num));
    return 0;
}


For the input : 5

Output:-

Cube of 5 is 125

Here we have our output.

So that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Do come back for more because learning paves way for a better understanding.

Keep Coding!! Happy Coding!! :)