Thursday 17 October 2013

Header Files in C and C++

Header files


You have already been using a header file from day-zero. You know that we used towrite at the top before the start of the main() function <iostream.h>, with ‘.h’ as an extension, you might have got the idea that it is a header file.
Now we will see why a Header file is used. In the previous lecture, we discussed a little bit about Function Prototypes. One thing is Declaration and other is Definition. Declaration can also be called as ‘Prototype’. Normally, if we have lot of functions and want to use them in some other function or program, then we are left with only one way i.e. to list the prototypes of all of them before the body of the function or program and they use them inside the function or program. But for frequent functions inside a program, this technique increases the complexity (of a program). This problem can be overcome by putting all these function prototypes in one file and writing a simple line of code for including the file in the program. This code line will indicate that this is the file, suppose 'area.h' containing all the prototypes of the used functions and see the prototypes from that file. This is the basic concept of a header file.

So what we can do is: Make our own header file which is usually a simple text file with '.h' extension ('.h' extension is not mandatory but it is but it is a rule of good programming practice).
Write function prototypes inside that file. (Recall that prototype is just a simple line of code containing return value, function name and an argument list of data types with semi-colon at the end.)
That file can be included in your won program by using the ‘#inclue’ directive and that would be similar to explicitly writing that list of function prototypes.

Function prototypes are not the only thing that can be put into a header file. If we use the standard  variables like ‘pi’ which is equal to 3.1415926 in a file and then include the file as a header file we will be able to use the variable ‘pi’ in any file which includes the containing file, without specifying every time the value of ‘pi’. It would be nice, if we can assign meaningful names to them. There are two benefits of doing this. See, we could have declared a variable of type double inside the program and given a name like ‘pi’:
double pi = 3.1415926;
Then everywhere in the subsequent calculations we can use 'pi'. But it is better to pre-define the value of the constant in a header file (one set for all) and simply including that header file, the constant ‘pi’, is defined. Now, this meaningful name ‘pi’ cab be used in all calculations instead of writing the horrendous number 3.1415926 again and again.
There are some preprocessor directives which we are going to cover later. Moment, we will discuss about ‘#define’ only. We define the constants using this preprocessor directive as:

#define pi 3.1415926

The above line des a funny thing as it is not creating a variable. Rather it associates a name with a value which can be used inside the program exactly like a variable. (Why it is not a variable?, because you can’t use it on the left hand side of any assignment.). Basically, it is a short hand, what actually happens. You defined the value of the ‘pi’ with ‘#difine’ directive and then started using ‘pi’ symbol in your program. Now we will see what a compiler does when it is handed over the program after the writing process. Wherever it finds the symbol ‘pi’, replaces the symbol with the value 3.1415926 and finally compiles the program. Thus, in compilation process the symbols or constants are replaced with actual values of them. But for us as human beings, it is quite readable to see the symbol ‘pi’. Additionally, if we use meaningful names for variables and see a line ‘2 * pi * radius’, it becomes obvious that circumference of a circle is being calculated. Note that in the above statement, ‘2 * pi * radius’; 2 is used as a number as we did not define any constant for it. We have defined ‘pi’ and ‘radius’ but defining 2 would be over killing.




No comments:

Post a Comment