The Structure of a Simple Program

I l @ ve RuBoard

The Structure of a Simple Program

Now that you've seen a specific example, you are ready for a few general rules about C programs. A program consists of a collection of one or more functions, one of which must be called main() . The description of a function consists of a header and a body. The header contains preprocessor statements, such as #include , and the function name. You can recognize a function name by the parentheses, which may be empty. The body is enclosed by braces { } and consists of a series of statements, each terminated by a semicolon (see Figure 2.4). The example in this chapter had a declaration statement , announcing the name and type of variable being used. Then it had an assignment statement giving the variable a value. Next, there were three print statements , each calling the printf() function. The print statements are examples of function call statements . Finally, main() ends with a return statement .

Figure 2.4. A function has a header and a body.
graphics/02fig04.jpg

In short, a simple ANSI C program should follow this format:

 #include <stdio.h> int main (void) {      statements      return 0; } 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net