Monday, July 2, 2012

CPro1 Lecture: Components of a C Program


2
The Components of a C Program
OBJECTIVE
Every C program consists of several components combined in a certain way.  Most of this manual is devoted to explaining these various program components and how you use them.  To get the overall picture, however, you should begin by seeing a complete (though small) C program with all its components identified.  You will learn
·  A short C program with its components identified.
·  The purpose of each program component.
·  To compile and run a sample program.

A Short C Program
Listing 2.1 presents a very simple program; all it does is input two numbers from the keyboard and calculate their product.  We will use this sample program to gain some familiarity with the parts of a C program.  Before looking at the sample program, you need to know what a function is, because functions are central to C programming.  A function is an independent section of a program code that performs a certain task and has been assigned a name.  By referencing a function’s name, your program can execute the code in the function.  The program also can send information, called arguments, to the function, and the function ca return information to the program.  The two types of C functions are library functions, which are part of the C compiler package, and user-defined functions, which you, the programmer, create.

2.1.  MULTIPLY.C
1: /* Program to calculate the product of two numbers */
2: #include <stdio.h>
3: int a,b,c;
4: int product(int x, int y);
5: main()
6: {
7:   /* Input the first number */
8:   printf(“Enter a number between 1 and 100:”);
9:   scanf(“%d”,&a);
10:      
11:  /* Input the second number */
12:  printf(“Enter another number between 1 and 100:”);
13:  scanf(“%d”,&a);
14: 
15:  /* Calculate and display the product */
16:  c = product(a, b);
17:  printf(“\n%d times %d = %d, a, b, c);
18: }
19:
20: /* Function returns the product of its two arguments */
21: int product ( int x, int y )
22: {
23:  return ( x * y);
24: }
The output for Listing 2.1 is
Enter a number between 1 and 100:  35
Enter another number between 1 and 100:  23
35 times 23 = 805

The Program Components
The main() Function (Lines 5 - 18)
The only component that is required in every C program is the main() function.  In its simplest form, the main() function consists of the name main followed by a pair of empty parentheses ( () ) and a pair of braces ( {} ).  Within the braces are statements that make up the main body of the program.  Under normal circumstances, program execution starts at the first statement in main() and terminates with the last statement in main().
#include Directive (Line 2)
The #include directive instructs the C compiler to add the contents of an include file into your program during compilation.  An include file is a separate disk file that contains information needed by the compiler.  Several of these files (sometimes called header files) are supplied with your compiler.  You never need to modify the information in these files -- that’s why they are kept separate from your source code.  Include files should all have the .H extension (for example, STDIO.H).
Variable Definition (Line 3)
A variable is a name assigned to a data storage location.  Your program uses variables to store various kinds of data during program execution.  In C, a variable must be defined before it can be used.  A variable definition informs the compiler of the variable’s name and the type data it is to hold.

Function Prototype (Line 4)
A function prototype provides the C compiler with the name and arguments of the function contained in the program and must appear before the function is used.  A function prototype is distinct from a function definition that contains the actual statements which make up the function.
Program Statements (Lines 8, 9, 12, 13, 16, 17, 23)
The real work of  a C program is done by its statements.  C statements display information on the screen, read keyboard input, perform mathematical operations, call functions, read disk files, and all other operations that a program needs to perform.  In C, statements are written one per line and always end with a semicolon.
printf()
The printf() statement (lines 8,12, and 17) is a library function that displays information on the screen.  The printf() statement can display a simple text message (as in line 8 and 12) or a message and the value of one or more program variables (as in line 17)
scanf()
The scanf() statement (lines 9 and 13) is another library function.  It reads data from the keyboard and assigns that data to one or more program variables.
c = product (a, b);
The program statement calls the function named product(). That is, it executes the program statements contained in the function product().  It also sends the arguments a and b to the function.  After the statements in product() are completed, product() returns a value to the program.  This value is stored in the variable named c.
return ( x * y);
The statement is part of the function product().  It calculates the product of the variables x and y and returns the result to the program that called product().
Function Definition (Lines 21-24)
A function is an independent, self-contained section of code that is written to perform a certain task.  Every function has a name, and the code in each function is executed by including that function’s name in a program statement.  This is known as calling the function.  The function named product(), in lines 21-24, is a user-defined function.  As the name implies, user-defined functions are written by the programmer during program development.  C also include library functions that are a part of the C compiler package.  Library functions perform most of the common tasks (such as screen, keyboard, and disk I/O) that your program needs.  In the sample program printf() and scanf() are library functions.
Program Comments (Lines 1, 7, 11, 15, 20)
Any part of your program that starts with /* and ends with */ is called a comment.  The compiler ignores all comments and so they have absolutely no effect on how a program works.  A comment can span part of a line, an entire line, or multiple lines.  You should not, however, use nested comment (which means you shouldn’t include one comment within another).
DO / DON’T
DO       add abundant comments to your program’s source code, especially near statements or functions that could be unclear to you or to someone who might have to modify it later.
DON’T  add unnecessary comments to statements that are already clear.  For example typing
       /* The following prints Hello World! on the screen */
       printf(“Hello World!”);
       may be going a little too far, at least once you’re completely comfortable with the printf() function and how it works.
DO       learn to develop a style that will be helpful.  A style that’s too lean or cryptic doesn’t help, nor does one that’s so verbose your spending more time commenting than programming!
Braces (Lines 6,18, 22, 24)
You use braces ( {} ) to enclose the program lines that make up every C function -- including the main() function.  A group of one or more statements enclosed within a braces is called a block.

1 comment: