Lesson 56 of 60 – Header Files in C
93%

Header Files in C

Header files in C contain declarations, macros, type definitions, and other information that can be shared between source files. They help organize programs into reusable and manageable components.

Note: Header files commonly use the .h extension. Standard headers are included using angle brackets, while project headers are commonly included using double quotes.

1. What is a Header File?

A header file is a file that usually contains declarations and definitions that can be shared by multiple C source files.

Examples include:

  • Function declarations
  • Macro definitions
  • Structure declarations
  • Enumeration declarations
  • Typedef declarations

2. Why Use Header Files?

Header files help divide a large program into smaller and reusable parts.

  • Improve code organization
  • Share declarations between files
  • Reduce repeated declarations
  • Support modular programming
  • Make projects easier to maintain

3. Standard Header Files

C provides many standard header files through its standard library.

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

Each header provides declarations for a particular group of standard library facilities.

4. stdio.h

The stdio.h header provides declarations for standard input, output, and file-related functions.

#include <stdio.h>

Common functions include:

  • printf()
  • scanf()
  • fopen()
  • fclose()
  • fgets()
  • fputs()

5. stdlib.h

The stdlib.h header provides general-purpose library facilities.

#include <stdlib.h>

Examples include:

  • malloc()
  • calloc()
  • realloc()
  • free()
  • atoi()
  • exit()

6. string.h

The string.h header provides functions for working with strings and memory blocks.

#include <string.h>

Common functions include:

  • strlen()
  • strcpy()
  • strcat()
  • strcmp()
  • strchr()
  • memcpy()

7. math.h

The math.h header provides mathematical functions.

#include <math.h>

Examples include:

  • sqrt()
  • pow()
  • ceil()
  • floor()
  • fabs()

8. ctype.h

The ctype.h header provides functions for character classification and conversion.

#include <ctype.h>

Examples include:

  • isalpha()
  • isdigit()
  • isalnum()
  • isspace()
  • toupper()
  • tolower()

9. time.h

The time.h header provides facilities for working with time and dates.

#include <time.h>

Examples include:

  • time()
  • localtime()
  • gmtime()
  • strftime()
  • clock()

10. Header File Inclusion

The #include preprocessor directive is used to include a header file.

#include <stdio.h>

The header's declarations become available to the source file after preprocessing.

11. Angle Brackets vs Double Quotes

#include <stdio.h>
#include "myheader.h"

Angle brackets are conventionally used for system or implementation-provided headers. Double quotes are commonly used for headers belonging to the current project.

12. Creating a Custom Header File

We can create our own header file.

myheader.h

#ifndef MYHEADER_H
#define MYHEADER_H

void greet(void);

#endif

The header contains the declaration of the greet() function.

13. Creating the Source File

The implementation of the function can be placed in a separate source file.

myheader.c

#include <stdio.h>
#include "myheader.h"

void greet(void)
{
    printf("Hello from custom header project!");
}

14. Using a Custom Header

The main source file can include the custom header and call the declared function.

main.c

#include "myheader.h"

int main()
{
    greet();

    return 0;
}

15. Header Guards

Header guards prevent a header's contents from being processed more than once in the same translation unit.

#ifndef MYHEADER_H
#define MYHEADER_H

void greet(void);

#endif

This is a common technique for preventing repeated declarations caused by multiple inclusions.

16. How Header Guards Work

The first time the header is included, MYHEADER_H is not defined, so the declarations are processed.

The macro is then defined. If the same header is encountered again, #ifndef MYHEADER_H becomes false and its contents are skipped.

17. #pragma once

Many compilers support:

#pragma once

It is commonly used to request that a header be included only once per translation unit.

Traditional include guards are also widely used and are part of portable preprocessor practice.

18. Function Declaration in Header

Function declarations are commonly placed in header files when the functions are implemented in another source file.

int add(int a, int b);

The corresponding implementation can be placed in a .c file.

19. Structure Declaration in Header

A header can contain structure declarations that need to be shared by multiple source files.

typedef struct
{
    int id;
    char name[50];
} Student;

Source files that include the header can then use the Student type.

20. Macro Definition in Header

A header can also contain project-wide macro definitions.

#ifndef CONFIG_H
#define CONFIG_H

#define MAX_STUDENTS 100
#define SCHOOL_NAME "Soopro Pathshala"

#endif

21. Header and Multiple Source Files

A larger C project can be divided into several source files.

project/
│
├── main.c
├── student.c
├── student.h
└── utility.c

The header can provide declarations shared by the source files.

22. Declaration vs Definition

A header commonly contains declarations, while the corresponding definitions may be placed in source files.

student.h

void displayStudent(void);

student.c

#include "student.h"

void displayStudent(void)
{
    printf("Student information");
}

23. Include Your Own Header

A project header can be included using double quotes.

#include "student.h"

This allows the source file to use declarations provided by the project header.

24. Header File with Multiple Declarations

#ifndef CALCULATOR_H
#define CALCULATOR_H

int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);

#endif

A single header can contain declarations for a group of related functions.

25. Common Mistake: Missing Header

If a function or type is used without the appropriate declaration being visible, the compiler may produce diagnostics.

For example, standard input/output functions should be used with:

#include <stdio.h>

Include the appropriate header before using the declarations it provides.

26. Common Mistake: Repeated Header Contents

A header that can be included through multiple paths should normally have an include guard.

#ifndef STUDENT_H
#define STUDENT_H

typedef struct
{
    int id;
} Student;

#endif

This helps prevent duplicate declarations within the same translation unit.

27. Complete Custom Header Example

calculator.h

#ifndef CALCULATOR_H
#define CALCULATOR_H

int add(int a, int b);
int multiply(int a, int b);

#endif

calculator.c

#include "calculator.h"

int add(int a, int b)
{
    return a + b;
}

int multiply(int a, int b)
{
    return a * b;
}

main.c

#include <stdio.h>
#include "calculator.h"

int main()
{
    printf("Sum = %d\n", add(10, 20));
    printf("Product = %d\n", multiply(10, 20));

    return 0;
}

28. Benefits of Custom Header Files

  • Organize related declarations
  • Support modular programming
  • Reuse declarations across source files
  • Make projects easier to maintain
  • Separate interfaces from implementations
  • Reduce repeated declarations
  • Improve project structure

29. Practical Header File Structure

A simple project can be organized like this:

student_project/
│
├── main.c
├── student.c
├── student.h
├── utility.c
└── utility.h

The .h files can contain shared declarations, while .c files can contain implementations.

30. Practice Project

Create a simple calculator project using separate files:

  1. Create calculator.h.
  2. Add declarations for addition, subtraction, multiplication, and division.
  3. Create calculator.c.
  4. Implement all four functions.
  5. Create main.c.
  6. Include calculator.h.
  7. Take two numbers from the user.
  8. Call the functions from calculator.c.
  9. Display the results.
  10. Use an include guard in the header file.

This project will help you understand header files, source files, function declarations, definitions, and modular programming.

📌 Key Points

  • Header files usually use the .h extension.
  • They commonly contain declarations, macros, and type definitions.
  • #include is used to include a header.
  • Standard headers are commonly written with angle brackets.
  • Project headers are commonly written with double quotes.
  • stdio.h provides standard input/output declarations.
  • stdlib.h provides general-purpose library declarations.
  • string.h provides string and memory functions.
  • Custom headers help organize large projects.
  • Include guards help prevent repeated header contents.
  • #pragma once is another commonly supported way to request single inclusion.
  • Function declarations can be placed in header files and implemented in source files.
  • Header files support modular and reusable C programming.

🧠 Quick Quiz

Question: Which preprocessor directive is commonly used to include a header file?