In this tutorial you will learn about the C Header Files and its application with practical example.
In C Programming Language Header Files gives us a way to reuse the external declarations, functions, or macro definition and allows us to share between other source files. When you include a header file, the compiler adds the functions, data types, and other information in the header file to the list of reserved words and commands in the language. We can include a header file in our program using C Preprocessor Directive “#include”, Header files almost always have a .h extension. Including a header file produces the same result as you copy the source code in your program.
In C Programming Language Headers files are used for the following purpose –
- It enables you to include system or operating system-related macro definitions and functions so that you can invoke system calls and libraries.
- You can create your own custom header files so that you can reuse and share them.
- It makes the program more manageable.
- It increases the portability of the program.
One of the most commonly used header files is for the standard input/output routines is called stdio. h.
We can include header files in the following two ways –
includes a header file from the current directory (the directory in which your C source code file appears), and
includes a file from a system directory.
Type Of Header Files
Standard Header Files
It is the set of header files used for performing various common and standard operations.
stdio. h – Defines core input and output functions
string.h – Defines string handling functions.
time.h – Defines date and time handling functions
math.h – Defines common mathematical functions.
User-Defined Header Files
In C Programming Language users can have their own custom header file that provides additional capabilities.
How to Create Your Own Header File in C Programming Language
Step #1
Type the below source code in a file
Put only function definition as you write in General C Program
Step #2
- Save Above Code with [.h ] Extension.
- Let the name of our header file be my head [ my head.h ]
- Compile Code if required.
Step #3
Write Main Program
- Include Our New Header File.
- Instead of writing < my head.h> use this terminology “my head.h”
- All the Functions are defined in the head.h header files are now ready for use.
- Directly call function to add(); [ Provide proper parameter and take care of return type ]
While running your program both files (my head. h and sample. c) should be in the same directory.