Sams Teach Yourself C in 21 Days

(singke) #1
details of how the program works. The point is for you to gain some familiarity with the
parts of a C program so that you can better understand the listings presented later in this
book.
Before looking at the sample program, you need to know what a function is,
because functions are central to C programming. A functionis an independent
section of 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
can return information to the main part of the program. The two types of C functions are
library functions,which are a part of the C compiler package, and user-defined functions,
which you, the programmer, create. You will learn about both types of functions in this
book.
Note that, as with all the listings in this book, the line numbers in Listing 2.1 are not part
of the program. They are included only for identification purposes, so don’t type them.
List 2.1 is available on the CD-Rom included with this book. You will find it in the
Day02 directory.

LISTING2.1 multiply.c—A program that multiplies two numbers
1: /* Program to calculate the product of two numbers. */
2: #include <stdio.h>
3:
4: int val1, val2, val3;
5:
6: int product(int x, int y);
7:
8: int main( void )
9: {
10: /* Get the first number */
11: printf(“Enter a number between 1 and 100: “);
12: scanf(“%d”, &val1);
13:
14: /* Get the second number */
15: printf(“Enter another number between 1 and 100: “);
16: scanf(“%d”, &val2);
17:
18: /* Calculate and display the product */
19: val3 = product(val1, val2);
20: printf (“%d times %d = %d\n”, val1, val2, val3);
21:
22: return 0;
23: }
24:

30 Day 2

NEWTERM

05 448201x-CH02 8/13/02 11:14 AM Page 30

Free download pdf