LEC20081103 - Introduction to Computer Programming November...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Computer Programming November 3, 2008 CSC180 Fall 2008, University of Toronto Multiple source files compilation A large C program has many .c files and .h files. What should be put in .c files? #include directives, external variables, and function definitions. What should be put in .h files? Macro definitions, type definitions, function prototypes, and external variable declarations. Include header files wherever needed using #include . Dont include .c files. Benefits: including header files guarantees that all source files are supplied with the same macro definitions, type definitions, external CSC180 Fall 2008, University of Toronto 1 variable declarations, and function prototypes. An update in a header file will propagate to all source files that include it. Functions or external variables that are not to be shared among source files should be declared static . For example, /* stack.c */ static int sp = 0; /* next free stack position */ static double val[MAXVAL]; /* value stack */ static void overflow(void) /* a static function */ { printf(stack is full\n); } For each .c file (e.g., foo.c ), we may create a corresponding .h file (e.g., foo.h ). In foo.h , we write macro definitions, type definitions, CSC180 Fall 2008, University of Toronto 2 external variable declarations, and function prototypes needed by foo.c . Benefits: wherever we use the functions defined in foo.c , we include foo.h thus the compiler could see their function prototypes and compile each source file separately. Include foo.h in foo.c . Benefits: the compiler checks that a functions prototype in foo.h matches its definition in foo.c . Example: divide the Reverse Polish calculator program into getop.h , getop.c , getch.h , getch.c , stack.h , stack.c , and calc.c : /* getop.h -- header for getop.c */ #ifndef GETOP_H #define GETOP_H #define NUMBER 0 /* signal that a number was found */ int getop(char ); #endif CSC180 Fall 2008, University of Toronto 3 /* getop.c -- implement getop *//* getop....
View Full Document

Page1 / 17

LEC20081103 - Introduction to Computer Programming November...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online