Header Files in C
0 194
Header files in C are used to declare the function prototypes, macros, and global variables that can be used across multiple source files.
They help in organizing the code, improving readability, and reducing redundancy.
Syntax of header file in C
The syntax to include a header file in C is:
#include<header_file_name.h>Here,
Types of Header Files
1
These are provided by the C Standard Library.
These provide the essential functions and constants required for standard operations like I/O, memory allocation, mathematical calculations, etc.
Examples: <stdio.h>, <stdlib.h>, <math.h>
2User-defined Header Files:
These are created by the programmer.
These are created by programmers to group related function prototypes, macros, or constants that are used across different source files in a project.
Examples: my_functions.h, constants.h
Using a User-defined Header File to Calculate Area of Rectangle
rectangle.h (Header File) #ifndef RECTANGLE_H #define RECTANGLE_H // Function prototype declaration double calculateRectangleArea(double length, double width); #endif // RECTANGLE_H
rectangle.c (Source File)
#include "rectangle.h" // Function definition double calculateRectangleArea(double length, double width) { return length * width; }
main.c (Main Program)
#include<stdio.h>#include "rectangle.h" int main() { double length = 5.0; double width = 10.0; double area; // Calculate rectangle area using function from rectangle.h area = calculateRectangleArea(length, width); printf("Length: %.2f\n", length); printf("Width: %.2f\n", width); printf("Area of rectangle: %.2f\n", area); return 0; }
rectangle.h (Header File):
This header file contains a function prototype (calculateRectangleArea) to calculate the area of a rectangle.
rectangle.c (Source File):
This source file contains the definition of the calculateRectangleArea() function, which calculates the area of a rectangle.
main.c (Main Program):
This is the main program file where the program execution starts.
It includes the rectangle.h header file to use the calculateRectangleArea() function.
Compilation and Execution:
Save the above code snippets in three separate files with the respective names (rectangle.h, rectangle.c, main.c).
Compile the program using a C compiler like gcc:
gcc main.c rectangle.c -o output
Run the compiled program:
./output
Output:
Length: 5.00 Width: 10.00 Area of rectangle: 50.00
Header Files in C with Descriptions
Header File
|
Description
|
Primary Functionalities
|
#include<stdio.h>
| Used for standard input-output operations. | printf(), scanf(), fprintf(), fscanf() |
#include<string.h>
|
Used for string manipulation and operations.
| strlen(), strcmp(), strcpy(), strcat() |
#include<iostream>
| Used for input-output operations in C++. | cin, cout, cerr, clog |
#include<math.h>
| Contains math functions for mathematical calculations. |
sqrt(), pow(), log(), sin(), cos()
|
#include<iomanip.h>
| Used for formatting output, especially for controlling decimal places. | setprecision(), setw(), setfill() |
#include<signal.h>
|
Used for signal handling in C programs.
| signal(), raise(), kill(), sigaction() |
#include<errno.h>
|
Provides error handling functionalities and macros related to errors.
| errno, strerror(), perror(), err(), errx() |
Share:
Comments
Waiting for your comments