Header Files in C
×


Header Files in C

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, is the name of the header file you want to include.

Types of Header Files

1Standard Header Files:

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()





Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments