Presentation is loading. Please wait.

Presentation is loading. Please wait.

15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.

Similar presentations


Presentation on theme: "15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source."— Presentation transcript:

1 15. WRITING LARGE PROGRAMS

2 Source 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).

3 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.

4 Header Files Header 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 0 Other files can access these macro definitions by using the directive #include "boolean.h"

5 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.

6 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.h header2.h includes header3.h prog.c includes header1.h and header2.h Header files can be protected against multiple inclusion: /* header3.h */ #ifndef HEADER3_H #define HEADER3_H … #endif

7 Compilation 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 functions.


Download ppt "15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source."

Similar presentations


Ads by Google