1 / 7

CSC 253

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

Télécharger la présentation

CSC 253

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 253 Lecture 6

  2. 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?

  3. 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.

  4. 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 …

  5. Out int_math.c • Let’s check parameters for validity … • Let’s code fact() recursively. • Suppose we compile int_math.c; what will happen?

  6. 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.

  7. 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 …

More Related