1 / 7

Module-Based Programming in C: Declarations vs. Definitions

This lecture explores the distinction between declarations and definitions in C programming, emphasizing their importance in module-based programming. We discuss the rationale behind separating declarations from definitions and outline the rules for their placement. Using our `int_math` module, we define two key functions: `pow(int base, int exp)` and `fact(int n)`. The lecture includes practical coding examples, validity checks for parameters, and an explanation of separate compilation. Finally, we show how to compile the program using `gcc` and integrate both object and source files for execution.

Télécharger la présentation

Module-Based Programming in C: Declarations vs. Definitions

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