i get confused when C books and papers talk about exposing interface (.h files) and hiding implementation.e.g when C Main program will wants access to data structure like queue, it will include queue.h
to its source. then they will talk about queue.c
being hidden from main program i.e main has no access to concrete queue types.
Now the question: What do they mean by implementation is hidden from main program and only accesses it through defined interface by header files
main program
------------
//call a function from .h file
Afunction();
function.h
----------
// prototypes
void Afunction(void)
function.c
---------
// concrete implementation
void Afunction(void) {
// your statements here..
{
I'm looking for answers even spanning compiler design if that will clear things for me.Thanks
C doesn't do a great job of hiding implementation details. You can't hide the structure of datatypes unless you are prepared to only export them as pointers. Still, that can go a long way.
Consider the standard FILE
datatype. The implementation of a FILE
will need to have a lot of details, including whatever the operating system requires to associate the object with an actual datastream, the current buffer and buffer location, the EOF and error flags, and a bunch of other things I can't think of right now, because I don't need to know.
Although I use FILE
s everyday, I don't need to think of any of those things. I just use FILE*
in ways provided by the I/O library, and don't worry about how the details are implemented. Even if I wanted to, I can't take out my monkey wrench and modify the internal data members. Which is good, because I would almost certainly get it wrong if I tried.
The division of programs into separate compilation units also means that I don't need to know anything about the internal functions used by the I/O library to implement its external API. Undoubtedly, it uses a host of internal functions, each of them with a name. If these names were visible to my code, I would have to make sure that I didn't use them for my own purposes by accident. Fortunately, I don't have to worry about that (mostly) because the names are (mostly) hidden.