Headers


Table of Contents

How to Create a Secure Header?

Securing a header (.h) doesn’t mean protecting it in a cybersecurity sense, but rather preventing it from being included multiple times during compilation.

Why Secure a Header File?

When a .h file is included in several modules, the compiler replaces each #include with the full content of the file, which can cause redefinition errors if the header is included multiple times.

Include Guards

To avoid this, we “secure” the header using include guards.
Let’s say the file is named “my_header.h”:

#ifndef MY_HEADER_H
#define MY_HEADER_H
    // Header content
#endif

These conditional directives ensure that the content of the file is compiled only once, even if it is included multiple times.

Directive Definition
IFNDEF Checks if a macro has not been defined yet (IFNDEF = If not defined)
DEFINE Defines the macro if it has not been defined (if it’s already defined, the block is ignored)
ENDIF Marks the end of the conditional block

What Is a Header File Made Of in C?

A header file (.h) groups together the declarations and definitions needed for proper code separation.

It improves reusability and readability within a project.

Function Prototypes

void my_function(void);

Constants and Macros

#define PI 3.14159 // Definition of an integer
#define STRING "This is a character string" // Definition of a string
#define ADD(a, b) (a + b) // Basic function returning a + b
#define IS_NEGATIVE(nb) (nb < 0 ? 1 : 0) // Function with a condition (ternary)

Declaration of External Variables

extern int global_counter;

Inclusion of Other Headers

#include <unistd.h>

Declaration of Structures, Unions, Enums

typedef struct s_myStruct
{
    int a;
    int b;
} t_myStruct;

Explanation:

The syntax above allows you to define a structure and create a type alias using the typedef keyword.

typedef // Used to create the alias
struct s_myStruct // Tag name of the structure
t_myStruct // Alias name

To improve readability and simplify the code, you can declare variables like this:

t_myStruct variable1;

// Instead of:
struct s_myStruct variable1;
How to Use or Manipulate a Structure?

To use the elements of a structure, you must first declare a variable (an instance).

Example:

void myFunction(void)
{
    t_myStruct var;
    
    var.a = 1;
    var.b = 2;
}

The variable of type “t_myStruct” contains the structure’s elements.
We use “var” to manipulate those elements.

Example of a Complete Header

#ifndef MY_HEADER_H
#define MY_HEADER_H

#define PI 3.14159 // Definition of an int
#define STRING "This is a character string" // Definition of a string
#define ADD(a, b) (a + b) // Basic function returning a + b
#define IS_NEGATIVE(nb) (nb < 0 ? 1 : 0) // Function with condition (ternary)

#include <unistd.h>

extern int global_counter;

void myFunction(void);

typedef struct s_myStruct
{
    int a;
    int b;
} t_myStruct;

#endif

Sources

Share :

Related Posts

Linked Lists

Linked Lists

Lists do not natively exist in C; they are the result of manipulating pointers and structures.

Read More
Makefiles

Makefiles

Make is used to manage executables and program compilation, and it is very effective for large projects because it recompiles only what has changed.

Read More