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 the header files and how we use them in our program to remove complexities from our code.

1. 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 making changes to only certain parts of the program makes it more susceptible to errors, but that is more like rewriting everything that is never beneficial and 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.

2. 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 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.

3. In-built header files

In-built header files are those which already exist 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.


4. User Defined Header Files

The name, user-defined header file is self-explanatory. C language offers a feature that enables us to create a header file and includes it in 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 "cube.h"
#include <stdio.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.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index