Thursday, May 9, 2013

C, Writing Large Programs

  This post is summary of Chapter 15 Writing Large Programs from "C Programming, A mordern approach" by K. N. King

15.2 Header Files
    1. What is Header file?
 ->  In computer programming, a header file is a file that allows programmers to separate certain elements of a program's source code into reusable files. Header files commonly contain forward declarations of classes, subroutines, variables, and other identifiers.  <http://en.wikipedia.org/wiki/Header_file
    2.  How do we call header files into other source files?
-> use #include 
-> header files have .h extension
    3. Two primary forms of the #include directive
-> #include <filename>    :   this is used for header files that belongs C's own library. Search the directory in which system header files reside
-> #include "filename"     :   this is used for all other header files including any that we write. Search the current directory, then search the directory in which system header files reside
    4. Sharing Macro Definitions and Type Definitions

example of sharing macro definitions and type definitions
          In this way, we can avoid repeating the same macro and type definitions.


    5. Sharing Function Prototypes
 -> Assume there is a source file containing many functions.  We want to call the functions into the other source file.  To avoid any possble errors, we need function prototypes.  The below illustrates how it is done.
-> To share a function, we put its definition in one source file, then put declarations in other files that need to call the function.  
Example of sharing Function Prototypes


    6. Sharing Variable Declarations
-> int i;                    /* declares i and defines it as well*/
-> extern int i;         /* declares i without defining it */
-> extern informs the compiler that i is defined elsewhere in the program.



    7.  Nested Includes
-> Assume that we have two functions that returns only 0 and 1, in other words, true and false.
-> I will use the previous example.  At the 5. Sharing Function prototypes, we had is_empty and is_full function.  Those two only returns 0 and 1.  So change declaring those at stack.h as
int is_empty(void)  => Bool is_empty(void) 
int is_full(void)      => Bool is_full(void)
  
-> to do this, we need to include the boolean.h file in the stack.h, so it becomes

    8. Protecting Header Files
-> If a source file includes the same header file twice, compilation errors may result. This problem is common when header files include other header files.  Consider the following diagram.
-> As you can see, when prog.c is compiled, file3.h will be compiled twice.
-> To prevent this happening, we protect a header file by enclosing the contents of the file in an #ifndef and #endif pair. Below is the example of protection of boolean.h file.
-> so when the boolean.h file is called twice, preprocessor checks if BOOLEAN_H is defined. And it defines BOOLEAN_H only if it hasn't been defined.


No comments:

Post a Comment