[C/C++]Creating And Using Header Files In C

What are header files??
In computer programming, header file is a file that allows programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers.

Programmers who wish to declare standardized identifiers in more than one source file can place such identifiers in a single header file, which other code can then include whenever the header contents are required. This is to keep the interface in the header separate from the implementation. The C standard library and C++ standard library traditionally declare their standard functions in header files.

The steps are as follows :

  • Type in the code.
  • Save the code with any suitable header file name and with .h extension instead of .cpp extension.
  • Compile the code.
  • Make another code and include your header file in it.
  • Use the functions of your sample header file.


Here's a simple example : The code for the header file...



This code will create two functions i.e. power(int,int) and fact(int) which are both int return type. They return the power to and factorial of a number.
Save this file as header.h or any other name with the .h extension and then compile it.

The code for using and checking whether the header file works....


In this code, remember to use #include"header.h" instead of #include if you have not created the header file in the BIN directory...

Here, I'd just created a simple header file to find find factorial and power to. You can include them in any program. You may create complex and hi-fi functions and use them...

Thats it
Enjoy..

0 comments:

Post a Comment

 

Copyright © 2011 Mbojo4Lab | Design by Kenga Ads-template