C Header Files

C provides a lot of header files to handle functions defined in the C standard library. The headers that relate to the functions you use in your programs are included using "#include." The most common header file is "stdio.h," which provides the type "file" that is necessary for disk file operations.

C Header Files List

The following table lists C header files along with their brief descriptions or uses.

Header File Use
<assert.h> defines the assert() macro
<ctype.h> character handling
<errno.h> error reporting
<float.h> defines implementation-dependent floating-point limits
<limits.h> defines various implementation-dependent limits
<locale.h> supports localization
<math.h> various definitions used by the math library
<setjmp.h> supports non-local jumps
<signal.h> supports signal handling
<stdarg.h> supports variable argument lists
<stddef.h> defines some commonly used constants
<stdio.h> supports the I/O system
<stdlib.h> miscellaneous declarations
<string.h> supports string functions
<time.h> supports system time functions
<complex.h> supports complex arithmetic
<fenv.h> gives access to the floating-point status flags and other aspects of the floating-point environment
<inttypes.h> defines a standard, portable set of integer type names. Also supports the functions that handle greatest-width integers
<iso646.h> defines macros that corresponds to various operators, such as && and ^
<stdbool.h> supports boolean data types, this defines the macro bool, which helps C++ compatibility.
<stdint.h> defines a standard, portable set of integer type names, this file included by the header file <inttypes.h>
<tgmath.h> defines type-generic floating-point macros
<wchar.h> supports multibyte and wide-character functions
<wctype.h> supports multibyte and wide-character classification functions

How to include a header file in a C program

The header file in a C program can be included using the "#include" preprocessor directive. The following is the general form for including a header file in your C program:

#include <filename.h>

Or,

#include "filename.h"

For example:

#include <stdio.h>

Or,

#include "stdio.h"

You can add your own or user-defined header files to your C program by using the second form.

Define and use your own header files in C

Creating and using your own header files simplifies programming. Let's suppose you are working on a large project and you require a lot of functions to use in your project. Then, if you put all the functions in a program, it becomes hard to read for others. And sometimes the programmer also becomes confused when viewing the code later. So to make a large project smaller, you can put all your functions in an external file with a ".h" extension, also called user-defined header files. Here is an example.

Let's first create a file, say myheader.h, and put the following code inside that file.

int square(int a)
{
   return a*a;
}

int sum(int a, int b)
{
   return a+b;
}

int subtract(int a, int b)
{
   return a-b;
}

void welcome(void)
{
   printf("Welcome to C Programming\n");
}

Now you have created your own header file named "myheader.h," which has the above functions. Now just concentrate on this program. Here we included the above header file, i.e., "myheader.h," and will use the function square(). Let's look at the following example program to understand it completely:

#include <stdio.h>
#include "myheader.h"

int main()
{
   int num;

   printf("Enter a number: ");
   scanf("%d", &num);
   printf("Square of %d is %d. \n", num, square(num));

   return 0;
}

As you can see, we have included the header file "myheader.h" and used the function square() of that header file to find the square of a number entered by the user. Here is a sample run of this C program.

c header file example

Now supply the input, say, 6, and hit the ENTER key to find and print the square of this number on the output console, just like shown in the snapshot given below:

c header file user defined example

Here is one more example program that also uses the same user-defined header file, i.e., "myheader.h," to use other functions defined in it.

#include <stdio.h>
#include "myheader.h"

int main()
{
   int num1, num2;

   welcome();

   printf("\nEnter two numbers: ");
   scanf("%d%d", &num1, &num2);

   printf("\n%d + %d = %d", num1, num2, sum(num1, num2));
   printf("\n%d - %d = %d \n", num1, num2, subtract(num1, num2));

   return 0;
}

The following snapshot shows the sample run of this C program with user inputs of 54 and 21 as two numbers.

custom header file example program in c

As you can see from the above two programs, we have created only one header file named "myheader.h" and used it two times. As a result, the program is simplified. You can also create your own header file and define all the functions inside it to use those functions in your program at any time in any program, just after including a header file, say "myheader.h."

C Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!