ben said:
Hi,
I have few doubts about header files.
Is it true that header files always have only "function declaration"
and not definition?
Not necessarily, but in general it's a good practice for several
reasons.
First of all, header files can wind up being #included multiple times
in the same translation unit (this often happens when header files
include other header files). Multiple definitions of the same function
in the same translation unit will cause the compiler to complain (it is
possible to avoid this problem with guard macros).
Secondly, if the header is included in several translation units, each
translation unit will have a redundant definition of that function.
Some linkers will resolve the redundant definitions into a single
definition, some may not. Some may complain about the redundant
definitions. If nothing else, the redundant definitions take up space.
If a function needs to be shared between multiple translation units,
it's better to put the function definition in a translation unit of its
own, compile it separately, and link it as necessary. That way, it
only needs to be compiled once (as opposed to once for every
translation unit that includes it), and you don't have to worry about
linker behavior.
Life is easiest when header files describe interfaces, not
implementations. There's really nothing stoppping you from putting a
function definition in a header file, but most of the time it will lead
to heartburn.
Next where can i find definition of functions defined in system header
files. I'm working on UNIX.
The actual source code may not be available for your implementation,
and if it is, it may vary widely between implementations. If you want
to see some examples of how the standard library functions *may* be
implemented, find P. J. Plauger's "The Standard C Library."