Header files in C

    Header files are built in toolkit which has many functions with its declaration, macro definitions that can be reused in all C programs.

Features:

It has many features as given below.

  • It is a modular programming.
  • It is easily readable.
  • It has good maintainability.
  • Reusability of code.

Syntax:

1.      Built in header file

       #include <headerfile.h>

      Where ‘.h’ is header file extension.

      Eg:

#include <stdio.h>

2.      User Defined header file.

#include “headerfile.h”

List of built in header files:

    <stdio.h> - It is for input and output functions.

    <stdlib.h> - It is for Utility functions.

    <string.h> - It is related to String manipulations.

    <math.h> - It has mathematical operations.

    <ctype.h> - it deals with character type.

    <time.h> -  it has time and date operations.

Let us create a c program to illustrate built in header file.

C Program to illustrate built in header file:

#include <stdio.h>

#include <math.h>

int main() {

    double number = 81.0;

    double result = sqrt(number);

    printf("The Square root of %.2lf is %.2lf\n", number, result);

    return 0;

}

While running this program, you get the following output.

The Square root 81.0 is 9.0

Next program shows the usage of user defined function.

User defined header file Program:

// sample.h

#ifndef sample

#define sample

x=10;

#endif

Then include it in your .c file:

#include "sample.h"

#include <stdio.h>

void main()

{

printf(“The number is “,x);

}

Output:

The number is 10.

That’s all. the different ways of using header files in C language is illustrated with programs.

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.