Monday, July 23, 2012

How to create our own header files in c

what are header files in c ?

Header files are the predefined files in c which consists the function definitions of the library functions. These header files are must and should be included in the program inorder to use inbuilt library functions in c. we can create our own header files in c to reduse code work.

Highly recommended for you:


what is the use of creating our own header files in c ?

header files are used to store the function definitions. so if your code is too large, then you can put dome of your function definitions in a header file and include that header file into your source code at the time of compilation. This reduces our coding work and increases code readability.

If there is any function which is to be repeatedly used in all the programs, then it becomes difficult to write the function definition in each and every program. for example if you want to display author information at the end of a program for all the programs written by a particular author, then it becomes hard to display it manually in each and every program.

so in that case you can store the author information in a function and save that function in a header file. Now you need to just include that header file in each and every program to display the author information.

how to create our own header files in c ?

creating our own header files in c is very easy. If you want to store the function definition of a particular function in a header file, then follow the below steps.

step 1:-

write the function definition in a text file and save it as filename.h. remember to write only function definition. you should not write function prototype and main function. remember the function name you used in the header file.


step 2:-

write the program by including the created header file.

also read: how to include a header file in the program.

output :-

sum is 15
about author
name: Kirti Kumar

 e-mail:kirti28kumar@gmail.com
website:https://programmer-choice.blogspot.com

In the above program we are calling the function about_author() function which is in the header file. we are not defining the function definition in the program. the compiler includes the function definition of that function from the header file. 

In the similar way we can create any header files and use it in any program.

1 comment: