70 likes | 177 Vues
CSC 253. Lecture 6. Declarations vs. Definitions. Why do we separate declarations from definitions? What is the rule for placing declarations and definitions? What is “module-based programming” in C?. Our int_math module. Let’s define a file that contains two functions
E N D
CSC 253 Lecture 6
Declarations vs. Definitions • Why do we separate declarations from definitions? • What is the rule for placing declarations and definitions? • What is “module-based programming” in C?
Our int_math module • Let’s define a file that contains two functions • pow(int base, int exp) • fact(int n) • The first function is essentially the same as we wrote in the 3rd week. • Let’s start with the header file.
Our int_math.h • What idiom do we need to use at the beginning of the file? • Let’s write the rest of the code …
Out int_math.c • Let’s check parameters for validity … • Let’s code fact() recursively. • Suppose we compile int_math.c; what will happen?
Separate Compilation • We can compile int_math.c even without writing a main program. • We just use the -c switch on our call to gcc • This produces an object file that can be used later.
The Main Program • Now let’s write the main program … • (exercise) • We can run the program by listing our source file, along with the precompiled object file. • We can also compile both files together …