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....
Thursday, August 28, 2008
Friday, August 15, 2008
declaration vs definition
These terms have been oftenly confused with each other.Reason behind this is we cant observe it (in case of variables) when we are working in a single file.Lets see why i say like that..let me first briefly explain these two terms...
1. declaration.... its just an acknowledgement to the compliler about the blueprint of some entity. Be it a varaible or a funtion.By putting blue print i just mean that..the necessary information(characteristics) about the entity. so here complier doesnt allocate any memory to the entity(Remember functions also need memory not only variables). It just takes the information and ok..nothing is done from compiler side as far as allocation issues are concerned!!.So the entity declared doesnt exits in memory till now. so what to do , our target is to use those variables and functions...so here we come up with definitions.
2.Definition...
lets observe it in a program.
twoterms.c
#include...........
/*include the things you need
,i am not writting them here*/
extern int globalvalue;//this the case of pure variable declaration
void myfunction1(int,float);
/*A function declaration...as there isnt any body for the function ,so no need of memory, obviosuly compiler doesnt need to allocate any memory for this one, very clever!!!*/
void main()
{
int i;//remember its a declration as well as definition
printf("the value of i is %d",i)//garbage value will be dsiplayed
int i2=0;//declaraions as well as initialization
}
1. declaration.... its just an acknowledgement to the compliler about the blueprint of some entity. Be it a varaible or a funtion.By putting blue print i just mean that..the necessary information(characteristics) about the entity. so here complier doesnt allocate any memory to the entity(Remember functions also need memory not only variables). It just takes the information and ok..nothing is done from compiler side as far as allocation issues are concerned!!.So the entity declared doesnt exits in memory till now. so what to do , our target is to use those variables and functions...so here we come up with definitions.
2.Definition...
This is the thing which brings some enitity into its real existance.The value is given to the variable(if u do it explicitly then its ok , your value will be assigned to the variable otherwise you will get the garbage value). So here the memory is allocated for the entity and from hereafter it is present in the memory.
lets observe it in a program.
twoterms.c
#include...........
/*include the things you need
,i am not writting them here*/
extern int globalvalue;//this the case of pure variable declaration
void myfunction1(int,float);
/*A function declaration...as there isnt any body for the function ,so no need of memory, obviosuly compiler doesnt need to allocate any memory for this one, very clever!!!*/
void main()
{
int i;//remember its a declration as well as definition
printf("the value of i is %d",i)//garbage value will be dsiplayed
int i2=0;//declaraions as well as initialization
}
Monday, August 11, 2008
Easter eggs in a software
An Easter egg is a hidden functionality(set of tasks which however may or may not be part of the main system,it totally depends on the programmer's wish..whatever he likes to do he will do,it means there is not any preset standard to enforce the programmer to provide some specific functionality.) in a software, Which is not directly visible to the users however depending on some specific events they come into action....Now we will illustrate it in a very simple way by taking an example of a html page. In this page, suppose i want to show a help section which needs to be some event specific..i mean some particular event will trigger it.
So i say if u type help anywhere in the page , You will be taken to the help section....so this is known to me only ...however when users will go through it they will be knowing it afterwards. so obviously this is something hidden and about which there is not any message or say informatiion.
Now use this code (You are taken to google help section after typing the word "help", so this is an easter egg of this webpage.) in understanding this term......here goes the code!
http://www.w3.org/TR/html4/loose.dtd">
var accumulator = new Array();
var match_it = new Array();match_it[0]= 72;
match_it[1] = 69;
match_it[2] = 76;
match_it[3]= 80;
var index = 0;
var started = 0;
window.onkeyup = keyup;
function clearString()
{index = 0;}
function keyUp(e)
{var mykeycode = e.keyCode;
var temp = parseInt(mykeycode);
accumulator[index] = temp;
index++;
if(index==4)
{
var matched = 0;
for(i=0;i<=3;i++)
{ if(accumulator[i]==match_it[i])
{
matched++; } } }
if(matched==4)
{ alert("you typed help");
window.location = "http://www.google.co.in/help" }
if(started==0)
{ var t = setInterval("clearString()",2500); started = 1; }
}
body onkeyup="javascript:keyUp(event);">
So i say if u type help anywhere in the page , You will be taken to the help section....so this is known to me only ...however when users will go through it they will be knowing it afterwards. so obviously this is something hidden and about which there is not any message or say informatiion.
Now use this code (You are taken to google help section after typing the word "help", so this is an easter egg of this webpage.) in understanding this term......here goes the code!
http://www.w3.org/TR/html4/loose.dtd">
var accumulator = new Array();
var match_it = new Array();match_it[0]= 72;
match_it[1] = 69;
match_it[2] = 76;
match_it[3]= 80;
var index = 0;
var started = 0;
window.onkeyup = keyup;
function clearString()
{index = 0;}
function keyUp(e)
{var mykeycode = e.keyCode;
var temp = parseInt(mykeycode);
accumulator[index] = temp;
index++;
if(index==4)
{
var matched = 0;
for(i=0;i<=3;i++)
{ if(accumulator[i]==match_it[i])
{
matched++; } } }
if(matched==4)
{ alert("you typed help");
window.location = "http://www.google.co.in/help" }
if(started==0)
{ var t = setInterval("clearString()",2500); started = 1; }
}
body onkeyup="javascript:keyUp(event);">
press and hold any key, then release it to fire the keyup event
Subscribe to:
Posts (Atom)