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.
.h
extension. Standard headers are included using angle brackets, while
project headers are commonly included using double quotes.
A header file is a file that usually contains declarations and definitions that can be shared by multiple C source files.
Examples include:
Header files help divide a large program into smaller and reusable parts.
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.
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()
The stdlib.h header provides general-purpose library
facilities.
#include <stdlib.h>
Examples include:
malloc()calloc()realloc()free()atoi()exit()
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()
The math.h header provides mathematical functions.
#include <math.h>
Examples include:
sqrt()pow()ceil()floor()fabs()
The ctype.h header provides functions for character
classification and conversion.
#include <ctype.h>
Examples include:
isalpha()isdigit()isalnum()isspace()toupper()tolower()
The time.h header provides facilities for working with time
and dates.
#include <time.h>
Examples include:
time()localtime()gmtime()strftime()clock()
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.
#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.
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.
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!");
}
The main source file can include the custom header and call the declared function.
main.c
#include "myheader.h"
int main()
{
greet();
return 0;
}
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.
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.
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.
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.
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.
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
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.
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");
}
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.
#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.
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.
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.
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;
}
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.
Create a simple calculator project using separate files:
calculator.h.calculator.c.main.c.calculator.h.calculator.c.This project will help you understand header files, source files, function declarations, definitions, and modular programming.
.h extension.#include is used to include a header.stdio.h provides standard input/output declarations.stdlib.h provides general-purpose library declarations.string.h provides string and memory functions.#pragma once is another commonly supported way to request single inclusion.Question: Which preprocessor directive is commonly used to include a header file?