The title "Why headers are necessary in c?", by this title its not meant at all that they are not necessary in c++. this sentence becomes specific in case of c just because of the loose control of c standard over this crucial issue,which makes c a little bit non-resistant to complilation errors.As we all know that C is not much concerned about function declarations.If you call a function without declaring it anywhere(of course it should have a defined block of code) then c wont have any problem with it, it will compile it correctly and you may get the correct results till you call the function with proper arguments. the last clause that "till you call the function with proper arguments " makes a point of interst, what happens if someone calls a function with bad arguments (different no of arguments and type mismatch), nothing!!! here our compiler acts like a dumb one...it just assumes that the argument's list it has been provided is correct and pass it to the function's actual implementation(definition) and at that place errors may occur and it will be very hard to detect.As compiler wasnt even having some other source to match the correctness of the arguments, so wat it does in the case explained earlier was completly justified.
take for an example
a function whose body is like this....
void myfunc(int a , float b){
/*some code*/
}
now suppose there is no declaration anywhere
and we call the function
myfunc(23,54);//compiler will claim no error ,considering there is a function which takes two integer arguments
so here we are passing both integer arguments,which may cause a subtle problem because float was expected as second argument.
here we come up with the 1st and very basic requirement of function declarations (As declarations are generally meant to be done in header files ,so talking about all this is relevant)
1.so to detect these type of errors and allow comipiler to do some type promotion if necessary right at the compile time
2.And the second and very known to all requirements is to save the typing time in making declarations and hence ensuring no typing errors too....
Now in case we are using libraries or simply says we are to call some external functions.
suppose there are two function with the same signatures.
lets suppose the function name is func1(int ,float), and there are two bodies of this function (i mean two differennt versions with same signature, means they are made by two different person or say same also and the name of the two functions was kept same either accidently or intentinally for the purpose of relevant name.) As here we are trying to illustrate the importance of the header files in this scenario so we will first consider the case of absense of any header file (obviosly those one which contain the declrartions for the two functions). So as the tendency of c compiler is if it dont find any declarations then in that case it doesnt have any problem with that it simply guesses the structure of the function's prototype by the calling place. so it wont have any problem , the source file be compiled successfully..no problem till this point..now when it comes the chance for linker to do its job, then what linker will do is just link the function whichever will be found at first (As stated earlier there are two functions of the same prototype) .Even up to this point problem doesnt get clear about which i am trying to pull the attention ,as we don't see any error (neither compilation error nor linker error) thats y.
Now answer the following questions....
1. which version did u intend to call (here we already know that we have two functions of the same name placed in different obj files and libraries).
2. Is it gauranteed that the version you called has been linked ,isnt there any possibility that the other version got linked instead of the desired one.
And if this type of problem occured then it will be very difficult to trace out.
NOW HOW THE INCLUSION OF HEADER FILES SOLVES THIS TYPE OF RARE BUT VERY DIFFICULT TO TRACE ERRORS.
very simple, if u include the header files for both of the functions then compiler will find a multi decalaration problem in the source code and will notify about it ...souce code will not be complied and hence in this way we will get notified very earlier about a big and diffiult to find problem.
Now we will see the essence of header files from the library's vendors point of view. will be continued....
Now we will understand from library's vendor's point of view..
If you are providing some library then (in case if you want to sustain the proprietary rights then you will never like to give the complete source code .) you will obviously provide that in the form of object files or compiled libraries.
Now 1. how your user will know that wats there in the library??
2. And you will very probably need some declarations for your functions to be called with proper arguments and allowing the compliler to do some necesaary optimizations in terms of casting etc..
so these two things can be fulfilled in a very convinient way by providing header files...in which user can see wat functions are there in the library he is using and how to call them....
No comments:
Post a Comment