1.2. The Structure of C ProgramsThe procedural building blocks of a C program are functions, which can invoke one another. Every function in a well-designed program serves a specific purpose. The functions contain statements for the program to execute sequentially, and statements can also be grouped to form block statements, or blocks. As the programmer, you can use the ready-made functions in the standard library, or write your own whenever no standard function fulfills your intended purpose. In addition to the standard C library, there are many specialized libraries available, such as libraries of graphics functions. However, by using such nonstandard libraries, you limit the portability of your program to those systems to which the libraries themselves have been ported. Every C program must define at least one function of its own, with the special name main( ): this is the first function invoked when the program starts. The main( ) function is the program's top level of control, and can call other functions as subroutines. Example 1-1 shows the structure of a simple, complete C program. We will discuss the details of declarations, function calls, output streams and more elsewhere in this book. For now, we are simply concerned with the general structure of the C source code. The program in Example 1-1 defines two functions, main( ) and circularArea( ). The main( ) function calls circularArea( ) to obtain the area of a circle with a given radius, and then calls the standard library function printf( ) to output the results in formatted strings on the console. Example 1-1. A simple C program// circle.c: Calculate and print the areas of circles #include <stdio.h> // Preprocessor directive double circularArea( double r ); // Function declaration (prototype form) int main( ) // Definition of main( ) begins { double radius = 1.0, area = 0.0; printf( " Areas of Circles\n\n" ); printf( " Radius Area\n" "-------------------------\n" ); area = circularArea( radius ); printf( "%10.1f %10.2f\n", radius, area ); radius = 5.0; area = circularArea( radius ); printf( "%10.1f %10.2f\n", radius, area ); return 0; } // The function circularArea( ) calculates the area of a circle // Parameter: The radius of the circle // Return value: The area of the circle double circularArea( double r ) // Definition of circularArea( ) begins { const double pi = 3.1415926536; // Pi is a constant return pi * r * r; } Output: Areas of Circles Radius Area ------------------------- 1.0 3.14 5.0 78.54 Note that the compiler requires a prior declaration of each function called. The prototype of circularArea( ) in the third line of Example 1-1 provides the information needed to compile a statement that calls this function. The prototypes of standard library functions are found in standard header files. Because the header file stdio.h contains the prototype of the printf( ) function, the preprocessor directive #include <stdio.h> declares the function indirectly by directing the compiler's preprocessor to insert the contents of that file. (See also the section "How the C Compiler Works," at the end of this chapter.) You may arrange the functions defined in a program in any order. In Example 1-1, we could just as well have placed the function circularArea( ) before the function main( ). If we had, then the prototype declaration of circularArea( ) would be superfluous, because the definition of the function is also a declaration. Function definitions cannot be nested inside one another: you can define a local variable within a function block, but not a local function. |