A C program consists of one or more program files, one of which contains the main( )
function, which acts as the driver of the program. When your program is large enough
to require several files, you should use encapsulation and data hiding techniques to
group logically related functions and data structures into the same files. Organize
your programs as follows:
Create a README file to document what the program does.
Group the main function with other logically related functions in a program
file.
Use module files to group logically related functions (not including the main
function).
Use header files to encapsulate related definitions and declarations of variables
and functions.
Write a Makefile to make recompiles more efficient.
README File
A README file should be used to explain what the program does and how it is
organized and to document issues for the program as a whole. For example, a
README file might include
All conditional compilation flags and their meanings.
Files that are machine dependent.
Paths to reused components.
Standard Libraries
A standard library is a collection of commonly used functions combined into one file.
Examples of function libraries include “stdio.h” which comprises a group of
input/output functions and “math.h” which consists of mathematical functions.
When using library files, include only those libraries that contain functions that your
program needs. You may create your own libraries of routines and group them in
header files.
Header Files
Header files are used to encapsulate logically related ideas; for example the header file
“time.h” defines two constants, three types, and three structures, and declares seven
functions needed to process time. Header files may be selectively included in your
program files to limit visibility to only those functions that need them.
Header files are included in C source files before compilation. Some, such as “stdio.h”
are defined system-wide, and must be included by any C program that uses the
standard input/output library. Others are used within a single program or suite of
programs.
Use #include for system include files.
Use #include “user_file” for user include files.
Contain in header files data definitions, declarations, typedefs, and enums that
are needed by more than one program.
Organize header files by function.
Put declarations for separate subsystems in separate header files.
If a set of declarations is likely to change when code is ported from one
platform to another, put those declarations in a separate header file.
Avoid private header filenames that are the same as library header filenames.
For example, the statement #include will include the standard library
math header file if the intended one is not found in the current directory.
Include header files that declare functions or external variables in the file that
defines the function or variable. That way, the compiler can do type checking
and the external declaration will always agree with the definition.
Do not nest header files. Use explicit #include statements to include each
header file needed in each program file.
In the prolog for a header file, describe what other headers need to be included
for the header to be functional.
Module Files
A module file contains the logically related functions, constants, types, data
definitions and declarations, and functions. Modules are similar to a program file
except that they don’t contain the main( ) function.
Makefiles
Makefiles are used on some systems to provide a mechanism for efficiently
recompiling C code. With makefiles, the make utility recompiles files that have been
changed since the last compilation. Makefiles also allow the recompilation commands
to be stored, so that potentially long cc commands can be greatly abbreviated.
The makefile
Lists all files that are to be included as part of the program.
Contains comments documenting what files are part of libraries.
Demonstrates dependencies, e.g., source files and associated headers using
implicit and explicit rules.
Standard Filename Suffixes
The suggested format for source code filenames is an optional prefix (e.g., to indicate
the subsystem), a base name, and an optional period and suffix. The base name
should be unique (length may vary depending on your compiler; some limit filenames
to eight or fewer characters) and should include a standard suffix that indicates the file
type. Some compilers and tools require certain suffix conventions for filenames.
Figure 3 lists some standard suffixes; or use those dictated by your compiler.
Program Files
A C program consists of one or more program files, one of which contains the main( ) function, which acts as the driver of the program. When your program is large enough to require several files, you should use encapsulation and data hiding techniques to group logically related functions and data structures into the same files. Organize your programs as follows:
README File
A README file should be used to explain what the program does and how it is organized and to document issues for the program as a whole. For example, a README file might include
Standard Libraries
A standard library is a collection of commonly used functions combined into one file. Examples of function libraries include “stdio.h” which comprises a group of input/output functions and “math.h” which consists of mathematical functions. When using library files, include only those libraries that contain functions that your program needs. You may create your own libraries of routines and group them in header files.
Header Files
Header files are used to encapsulate logically related ideas; for example the header file “time.h” defines two constants, three types, and three structures, and declares seven functions needed to process time. Header files may be selectively included in your program files to limit visibility to only those functions that need them.
Header files are included in C source files before compilation. Some, such as “stdio.h” are defined system-wide, and must be included by any C program that uses the standard input/output library. Others are used within a single program or suite of programs.
Module Files
A module file contains the logically related functions, constants, types, data definitions and declarations, and functions. Modules are similar to a program file except that they don’t contain the main( ) function.
Makefiles
Makefiles are used on some systems to provide a mechanism for efficiently recompiling C code. With makefiles, the make utility recompiles files that have been changed since the last compilation. Makefiles also allow the recompilation commands to be stored, so that potentially long cc commands can be greatly abbreviated. The makefile
Standard Filename Suffixes
The suggested format for source code filenames is an optional prefix (e.g., to indicate the subsystem), a base name, and an optional period and suffix. The base name should be unique (length may vary depending on your compiler; some limit filenames to eight or fewer characters) and should include a standard suffix that indicates the file type. Some compilers and tools require certain suffix conventions for filenames. Figure 3 lists some standard suffixes; or use those dictated by your compiler.