Sams Teach Yourself C in 21 Days

(singke) #1
Advanced Compiler Use 595

21


5: long sqr(int x)
6: {
7: return ((long)x * x);
8: }

LISTING21.3 calc.h: the header file for calc.c
1: /* calc.h: header file for calc.c. */
2:
3: long sqr(int x);
4:
5: /* end of calc.h */

Enter an integer value: 100
The square of 100 is 10000.
Let’s look at the components of these three files in greater detail. The header file,
calc.h, contains the prototype for the sqr()function in calc.c. Because any mod-
ule that uses sqr()needs to know sqr()’s prototype, the module must include calc.h.
The secondary module file, calc.c, contains the definition of the sqr()function. The
#includedirective is used to include the header file, calc.h. Note that the header file-
name is enclosed in quotation marks rather than angle brackets. (You’ll learn the reason
for this later today.)
The main module, list2101.c, contains the main()function. This module also includes
the header file, calc.h.
After you use your editor to create these three files, how do you compile and link the
final executable program? Your compiler controls this for you. At the command line,
enter
xxxlist2101.c calc.c
wherexxxis your compiler’s command. This directs the compiler’s components to per-
form the following tasks:


  1. Compile list2101.c, creating list2101.obj (or list2101.o on a UNIX system). If it
    encounters any errors, the compiler displays descriptive error messages.

  2. Compile calc.c, creating calc.obj (or calc.o on a UNIX system). Again, error mes-
    sages appear if necessary.


INPUT

LISTING21.2 continued

INPUT/
OUTPUT

ANALYSIS

33 448201x-CH21 8/13/02 11:16 AM Page 595

Free download pdf