Hello everyone, This is my first post to dev.to in which I’m excited to share some insights on Header Files in C. I hope you find this discussion useful and informative
The C language is famous for its various libraries and predefined functions pre-written within it. These make the programmer's effort a lot easier.
What are the Header Files
Header files are additional files in a C language containing definitions of different functions and their associated variables that need to be imported into a C program with the help of a preprocessor #include statement. All the header files have a '.h' extension that contains C function declarations and macro definitions. The default header file that comes with the C compiler is the stdio.h.
The basic syntax of using these header files is:
#include<stdio.h>
There are two types of header files in C
- Standard Header Files in C
- User-defined Header Files in C
Standard Header Files in C
Standard header files contain the libraries defined in the ISO standard of the C programming language. They are stored in the default directory of the compiler and are present in all the C compilers from any vendor.
There are 31 standard header files in the latest version of C language.
Let’s have a look at some standard header files to have a better understanding:
- <stdio.h> : The very basic one which enables input and output.
- <math.h> : Allows us to use the Mathematical functions
- <conio.h> : Has certain in-built functions like clrscr(), getch(), etc.
- <complex.h> : Complex number arithmetic
- <float.h> : Contains constants related to floating point values.
- <tgmath.h> : Combines math.h and complex.h
- <time.h> : time/date utilities
- <stdbool.h> : Boolean datatype
- <signal.h> : Signal handling
User-defined Header Files in C
User-defined header file is self-explanatory. C language offers a feature that enables us to create a header file and includes it in our code.
User-defined header files in C enable you to declare functions, macros, and types that can be shared across multiple source files, enhancing modularity and code reusability. These files typically have a .h extension and contain declarations, while the corresponding definitions are placed in .c files.
-
Header File Creation:
Create a .h header file (e.g., my_header.h) and add function prototypes, macros, and type definitions. Use include guards (#ifndef, #define, #endif) to prevent multiple inclusions, which could lead to errors.
Example:#ifndef MY_HEADER_H #define MY_HEADER_H void myFunction(); #define PI 3.14159 typedef struct { int x; int y; } Point; #endif
Including the Header:
In your source files, include the header using #include "my_header.h". This allows you to access the declared functions, macros, and types.
- Function Definitions:
Implement the functions declared in the header within a .c file (e.g., my_functions.c).
Example:
#include "my_header.h"
void myFunction() {
printf("Function defined in a user-defined header file.\n");
}
- Compilation:
Compile the source files together (e.g., gcc main.c my_functions.c -o my_program), linking the code to create an executable.
Top comments (1)
A better article is here.