C Header Files

In C programming language a header file is a file containing C function declarations and micro definitions to be shared between several source files.

The extension of header file is '.h'. In C program we can request to include header files by using C preprocessor directive #include. For example like the stdio.h header file, which comes along with the compiler.

Including a header file means that using the content of header file in the source program. A straightforward practice while programming in C language is that we can keep every macro, global variables, constants, and other function prototypes in the header files. The basic syntax of using these header files is:
 #include <head_file_name>
or
 #include "header_file_name"
Where the header_file_name is name of the header file.

Example :
 #include <stdio.h>
 #include "headerfile.h"
Also note that in first syntax the < > sign is used to include header file and in the second syntax " " (double column) sign. The main difference between these two syntax is that, if header is included within <>, then the preprocessor will search a predetermined directory path to locate the header file. And if the header file is enclosed in "", the preprocessor will look for the header file in the same directory as the source file.

Custom Header file or User-Defined Header file :

Now lets see an example c program with custom header file. For example :

Filename : myheader.h
#define PI 3.14

float area(int rad) {
  float ans = 2*PI*(rad*rad);
  return ans;
}
Filename : test.c
#include<stdio.h>
#include "myheader.h"

int main() {
  int rad = 5;
  printf("Area : %.2f\n", area(5));
  return 0;
}
Output:

Area : 157.00


Next Topic :