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 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 header file produces the same result as you copy the source code in your program.
In C Programming Language Headers file are used for following purpose –
- It enables you to include system or operating system related macro definition and function, so that you can invoke system calls and libraries.
- You can create your own custom header files so that you can reuse and share it.
- It makes program more manageable.
- It increase the portability of program.
One of the most commonly used header file is for the standard input/output routines is called stdio.h.
We can include header files in 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 those 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 user can have their own custom header file that provide additional capabilities.
How to Create Your Own Header File in C Programming Language
Type the below source code in a file
int add(int a,int b)
Put only function definition as you write in General C Program
- Save Above Code with [.h ] Extension .
- Let name of our header file be myhead [ myhead.h ]
- Compile Code if required.
Write Main Program
number3 = add(number1,number2);
printf("Addition of Two numbers : %d",number3);
- Include Our New Header File .
- Instead of writing < myhead.h> use this terminology “myhead.h”
- All the Functions defined in the myhead.h header file are now ready for use .
- Directly call function add(); [ Provide proper parameter and take care of return type ]
While running your program both files (myhead.h and sample.c) should be in same directory.