Difference between header file and library file in c

Status
Not open for further replies.

chandu.kurapati

Full Member level 3
Joined
Oct 31, 2013
Messages
186
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Location
Bengaluru, India
Visit site
Activity points
2,778
Hi ,
Can any one explain about the what is the header file? & what is the library file? in c programming,
and please explain about what are the differences between them?

Thanks & Regards,
Chandu.Kurapati.
 

Although it is theoretically possible to substitute one for the other, they are normally used like this:

A header file contains definitions needed by the program it is related to. It may contain definitions for variables and functions within it's companion '.c' file or external definitions of variables or functions in another file used in the project. When you write very large programs which are generally spilt into several source files, it's easier to include a common header file into each of them than to add all the definitions into the text of each source. It also allows you to change only one instance of a definition for all the files using it to be updated simultaneously.

A library is usually a file of pre-compiled functions, normally to do with a particular topic. For example you might have a library file called 'comms.lib' that holds all your communication routines. The idea is that the library can be used by other programs without you having to rewrite the code again.

You add the library files you need and the source program you have written to your project and after compiling, the linker program moves the routines around to find the best fit in the available memory then fixes the addresses of the routines, both from your source and the library so they access the correct parts of the code.

Brian.
 
Shortly, you can think a header as the interface to a library, with no implementation, just with the function signature that you can call in your program. When this function is called by the program the compiler links to the source code in the library.

You can have lots of libraries that implements only one header file, i.e., stdio.h is the header file and glibc AND uclibc are libraries with all stdio.h functions implementation with differences between them.

I don't know if it would be important to you now, but it's important to know: libraries can be statically-linked or dynamically-linked, where the first one stands for a library that is copied into your final executable and is loaded into the memory all with your program just as one program, and only this program will use this copy. The second is linked outside the program at some point of memory where every program that uses this library will search for and execute the source.

Statically-linked is faster than dynamically because the program doesn't need look for the library in another block of memory and load it, but your final executable would be much larger.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…