All About Header Files in C

All About Header Files in C

This article talks about everything you need to know to work with header files in the C programming language. It explains:

  • What header files are
  • What header files contain
  • Importance of header files
  • How to make header files for your programs

This article is suitable for learners who have a basic understanding of C (knowledge of variables, loops and conditional statements) and can make a simple program with the language. After reading this article, you will be able to:

  • Explain the working and usage of header files
  • Use the already available header files
  • Make and use your own header files

What are header files?

A header file is a file containing C declarations and macro definitions to be shared between several source files. A header file contains C-language definitions and structures.

Types of header files

There are two types of header files:

1. C standard library header files

The C standard headers are predefined header files readily available in the C compiler.

2. User-defined header files

User-defined header files, on the other hand, are user-developed for use in a specific situation.

Why do we use header files?

  • Information that is needed by several different files or functions is collected into a header file. Centralizing information into a header file facilitates the creation and update of programs. Because #include statements are used to insert header files into a C-language program.

  • Including a header file produces the same results as copying the header file into each source file that needs it. Such copying would be time-consuming and error-prone. With a header file, the related declarations appear in only one place. If they need to be changed, they can be changed in one place, and programs that include the header file will automatically use the new version when next recompiled.

  • The header file eliminates the labour of finding and changing all the copies as well as the risk that a failure to find one copy will result in inconsistencies within a program.

  • They are most often used to include external variable declarations, macro definitions, type definitions, function declarations...etc

How do we use header files?

  1. You request the use of a header file in your program by including it, with the C preprocessing directive #include. Header files can include any legal C source code.

  2. Groups of logically related functions are commonly declared together in a header file, such as the C library input and output functions listed in the stdio.h header file. I believe you have seen stdio.h somewhere as you work with C.

  3. Header files traditionally have a .h suffix ( stdio.h , for example). It is most portable to use only letters, digits, dashes, and underscores in header file names, and at most one dot.
    The names of header files must not include the ' , \ , " , or / characters, because the use of these punctuation characters in a header file is undefined.

What does a header file contain?

Header files contain the following:

  • Include guards
  • Functions prototypes
  • Defined variables, macros, structures

How to Include a Header File

To use the functions, data types, and macros defined in a header file, you must import them to your program. To import a header, use the #include, a preprocessor directive telling the compiler that it should import and process the code before compiling it to the rest of the code. On a typical C program, it should contain the stdio.h header file, which is the standard header file for input and output streams. When referenced in a program, header names are surrounded by angle brackets or double quotation marks , as shown in the following example:

If the file is imported use #include <math.h> If the file is present locally use a relative path reference #include "path/to/file.h"

How does a header file look like?

An example structure of a header file header.h

#ifndef HEADER_H
#define HEADER_H

#include <stdio.h>
#include <stdlib.h>

/**
 * struct dlistint_s - doubly linked list
 * @n: integer
 * @prev: points to the previous node
 * @next: points to the next node
 *
 * Description: doubly linked list node structure
 */
typedef struct dlistint_s
{
    int n;
    struct dlistint_s *prev;
    struct dlistint_s *next;
} dlistint_t;

/* Functions prototypes */
size_t print_dlistint(const dlistint_t *h);
size_t dlistint_len(const dlistint_t *h);

#endif /* HEADER_H */