1

I am not sure why I need dictionary.h for . I feel like I can define the functions check, load, size, and unload in dictionary.c.

dictionary.c is set up so I can define the functions in it without using the header file at all. Why wouldn't I do this and keep all of the code in one file?

1
  • functions are not defined in header files (.h files). they're defined in implementation files (.c files).
    – kzidane
    Commented May 24, 2015 at 0:15

1 Answer 1

3

speller is a program with multiple source code files (speller.c, dictionary.c, and you may even have your own additional source code files). dictionary.h is a header file and a header file is just an interface. So dictionary.h is an interface for the functionality provided by dictionary.c.

To be able to use this functionality in other source code files, you need to have the appropriate declarations and definitions before you use it. Having a header file for that purpose typically makes life easier as you don't have to copy/paste the declarations and the definitions across all the source code files that use the functionality of dictionary.c, you don't have to worry about handling duplicated declarations, etc.

Without having a header file, it could be a pain if you later decided not to use some function/construct or maybe to add a helper function/construct as you'd have to go to all the files that use this functionality and modify them one by one (e.g., add/remove function declarations).

So thanks to header files, all you have to do is to include dictionary.h, modify it as necessary, and there you have everything set up in all the other files that use it.

Also separating definitions from the interface in such a way is an illustration of an important computer science concept, namely information hiding (aka encapsulation).

Watch the short on libraries for more info!


Update: answering the question in the comment

when you use functions like load, ... in speller.c and include dictionary.h atop of it, you're essentially telling the compiler "just treat these functions as if they are defined and I promise I'll define them later".

when you then compile the program, and per the Makefile, you're compiling both speller.c and dictionary.c and linking the object (.o) files of them together.

at this point, the linker looks for the definitions of the functions that you promised to define and finds them in dictionary.o so it provides a reference to them.

6
  • So line 46 in speller.c uses the function load. I thought I needed to write the code for this function in dictionary.h so it was available to both speller.c and dictionary.c. If I am reading your response correctly, I should be writing the code for load in dictionary.c and it will be available to speller.c because line 27 of dictionary.h which includes the declaration for the function load.This doesn't seem right. Commented May 24, 2015 at 10:43
  • @BrendanRafferty, this is exactly right! when you use functions like load, ... in speller.c and include dictionary.h atop of it, you're essentially telling the compiler "just treat these functions as if they are defined and I promise I'll define them later". when you then compile the program, and per the Makefile, you're compiling both speller.c and dictionary.c and linking the object (.o) files of them together. at this point, the linker looks for the definitions of the functions that you promised to define and finds them in dictionary.o so it provides a reference to them.
    – kzidane
    Commented May 24, 2015 at 12:34
  • @BrendanRafferty, the short on compilers may help too!
    – kzidane
    Commented May 24, 2015 at 12:36
  • Does this mean that when clang compiles with source files speller.c and dictionary.c, the two source files become integrated as #include does with libraries and header files? From my understanding, we can treat the functions implemented in dictionary.c as if they were implemented after the main function, just as we can treat the prototypes in dictionary.h as if they were prototypes defined before the main function? Would this work even though the .c file and .h file had different names (i.e. renaming dictionary.h to prototypes.h, though there wouldn't be a legitimate reason for doing so)?
    – user7269
    Commented Jun 2, 2015 at 0:29
  • @AstaB00M, not really. speller.c is compiled into speller.o. dictionary.c is compiled into dictionary.o. then the .o files are linked together by a program called the linker to produce the executable which is named speller (i.e., the one we use to execute the program). so more generally, we treat the functions that are called in speller.c as if they are defined later somewhere and leave it to the linker to find where these functions are defined and somehow put a reference to them where they're called.
    – kzidane
    Commented Jun 2, 2015 at 12:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .