The Header file in C

The Header file in C

Why and what is a Header file?

A header file is a file that contains declarations for various structures such as functions, variables, macros, and other constructs used in C programs. The "#include" preprocessor directive is used to include header files, which usually have a ".h" file extension.

In C programs, headers offer a means to separate the interface from the implementation. You can shield users of your code from implementation specifics and give them a clear interface to work with by including declarations in header files. Examples of header files include "stdio.h" (for standard input and output functions), "stdlib.h" (for memory allocation and other utility functions), "math.h" (for mathematical functions), and "string.h" (for string manipulation functions).

To use a header file in a C program, you typically include it at the top of your source code file using the #include directive, then your header filename like this:

#include <headerfilename>

Your other code goes down  here

How headers work and how they are used

  1. The first one is the one you authored in your program or code known as a user-defined/designed header. It is included like this:

     #include "header"
    

    The preprocessor searches for a header file named “headerfile” in your current source file directory.

  2. The second is the one that exists in your compiler known as Standard C library header file It is included like this:

     #include <headerfile>
    

    The preprocessor searches for a header file named “headerfile” in a collection of systems directories.

During the preprocessing stage/ phase, the “#include” directive includes the header files for scanning as part of the source file. It is scanned in as if the content of the header file was included in that position in the source code. For example, if your header file has this content:

#ifndef AREAOFQUADRILATERAL_H
#define AREAOFQUADRILATERAL_H
/**
*function prototype for area function
*to calculate area of quadrilaterals
*/ 
double area(double lenght,double width);
#endif

and you have this in your source file:

#include "Areaofquadrilateral"
/**
*function implementation for area function
*takes length and width of rectangle as inputs
*returns the area of the rectangle as an output
*/
double area(double lenght,double width){
return lenght * width;
}
int main(){
int x = area(3 , 2);
printf("The area of is %f" , x );

}

This is also the same as having :

double area(double lenght,double width);
/* function decleration from the header*/

double area(double lenght,double width){
return lenght * width;
}
/* function implementation */

int main(){
int x = area(3 , 2)
printf("The area of is %f" , x );
}

or it can also be wrapped in the conditionals/ processing directives you will learn more about this later on.

#ifndef AREAOFQUADRILATERAL_H
#define AREAOFQUADRILATERAL_H
/**
*function prototype for area function
*to calculate area of quadrilaterals
*/ 
double area(double lenght,double width);
#endif

double area(double lenght,double width){
return lenght * width;
}
/* function implementation */

int main(){
int x = area(3 , 2)
printf("The area of is %f" , x );
}

Showing it’s the same as the other two instances. Please you should note that while more than one header file is okay, multiple inclusion of the same header file will give a compiler error. The entire header file context can be wrapped in conditional preprocessor instruction just like this:

#ifndef NAME_OF_HEADERFILE_H
#define NAME_OF_HEADERFILE_H

All the content _for your header file goes in here;

#endif

When the header is included again during preprocessing the condition returns false the compiler will see it as having already been defined and skip its context.

Instead of using multiple conditional preprocessor instructive in scenarios that require multiple headers like this:

#ifndef MY_HEADER_H
#define MY_HEADER_H

#if WIN_OS
   # include "\\.h"
#elif I_OS
   # include "\.h"
#elif linux
   ...

#endif

You can use this:

#ifndef MY_HEADER_H
#define MY_HEADER_H

#define SYSTEM_OS "system_1.h"
...
#include SYSTEM_OS

Usage of Standard headers

Some list of standard C Headers and exemplary applications are;

  1. stdio.h standard
    Input/Output functions – helps us to perform input and output operations in C.

2. conio.h
Console Input/Output functions – used for consoling functions like clrscr(), getchar().

3. stdlib.h
General utility functions – used for string conversion, memory management, etc.

4. math.h
Mathematics functions- used for mathematical functions like nan(), nanf()

5. ctype.h
Character handling functions- used to test characters.

6. string.h
String functions- used for string

Example

#include <stdio.h>  // standard input-output header
#include <ctype.h>  // character handling functions header
#include <string.h> // string handling functions header
#include <stdlib.h> // standard library header
#include <conio.h>  // console input-output header

int main() {
    char str1[50], str2[50]; 
/*two character arrays string 1 and string 2*/ 
    int i, len; 
/*declear an integer i*/

    /* input string 1*/
    printf("Enter a string: ");
    gets(str1);

    /* copy string 1 to string 2 */
    strcpy(str2, str1);

    /* capitalize first letter of each word in string 2 */
    len = strlen(str2);
    for (i = 0; i < len; i++) {
        if (i == 0 || isspace(str2[i - 1])) {
            str2[i] = toupper(str2[i]);
        }
    }

   /* print original string and modified string */
    printf("Original string: %s\n", str1);
    printf("Modified string: %s\n", str2);

    /* pause the program so the output can be seen */
    getch();

   /* exit the program */
    return 0;
}

The Output

if you enter "hello world" then the consoles output should be:

Enter a string: hello world
Original string: hello world
Modified string: Hello World

Reference

  1. https://www.tutorialspoint.com/cprogramming/c_header_files.htm#:~:text=A%20header%20file%20is%20a,that%20comes%20with%20your%20compiler

  2. https://www.geeksforgeeks.org/header-files-in-c-cpp-and-its-uses/