Unformatted text preview:

15. WRITING LARGE PROGRAMSSource FilesSlide 3Header FilesSlide 5Protecting Header FilesCompilation and Linking15. WRITING LARGE PROGRAMSSource Files• A program may be divided into any number of source files.• Source files have the extension .c by convention.• Source files contain definitions of functions and variables.• One source file must contain a definition of main (the program’s entry point).Source Files• Dividing a program into multiple files has several advantages:Related functions can be grouped together into a single file, making the structure of the program clearer.Each source file can be compiled separately.A single source file can be shared by several programs.Header FilesHeader files contain information to be shared among several source files.• Header files are a convenient place to put macro definitions. A header file named boolean.h might contain the following lines:#define BOOL int#define TRUE 1#define FALSE 0Other files can access these macro definitions by using the directive#include "boolean.h"Header Files• Putting macro definitions in a header file has several advantages:We don’t have to copy the definitions into each source file.Changing the definition of a macro requires only that we edit a single header file.We don’t have to worry about the possibility that different source files might contain different versions of the macro.Protecting Header Files• If a source file includes the same header file twice, compilation errors may result, depending on the contents of the header file.• This problem is common when header files include other header files:header1.h includes header3.hheader2.h includes header3.hprog.c includes header1.h and header2.h• Header files can be protected against multiple inclusion:/* header3.h */#ifndef HEADER3_H#define HEADER3_H…#endifCompilation and Linking• Before a program can be executed, its source files (not its header files) must be compiled to produce object files. The object files must then be linked.• Compilation leaves “gaps” that are filled in during linking. For example, a source file may contain calls of functions that are not defined in that file.• The linker checks that all necessary functions are present (either in a source file or in the library).• Calls of functions in the C library are resolved by the linker; the compiler knows nothing about these


View Full Document

UF CGS 3460 - WRITING LARGE PROGRAMS

Download WRITING LARGE PROGRAMS
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view WRITING LARGE PROGRAMS and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view WRITING LARGE PROGRAMS 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?