LEC20080929 - Introduction to Computer Programming Required...

Info iconThis preview shows pages 1–7. 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 Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right 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 September 29, 2008 Required readings: chapter 9. CSC180 Fall 2008, University of Toronto External variables • Declared outside of any function • Remain in memory until the program finishes • Globally accessible by functions • Useful when many functions need to share variables • Example: /* file1.c */ int leap; CSC180 Fall 2008, University of Toronto 1 char daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; char *name[13] = {‘‘Illegal month’’, ‘‘Jan’’, ‘‘Feb’’, ‘‘Mar’’, ‘‘Apr’’, ‘‘May’’, ‘‘Jun’’, ‘‘Jul’’, ‘‘Aug’’, ‘‘Sep’’, ‘‘Oct’’, ‘‘Nov’’, ‘‘Dec’’ }; /* file2.c */ #include <stdio.h> extern char *name; /* can omit array size */ extern char daytab[13]; /* can omit first dim */ extern int leap; void set_leap(int year) { leap = year%4 == 0 && year%100 != 0 || year%400 == 0; } CSC180 Fall 2008, University of Toronto 2 void print_days(int n) { printf(‘‘%s has %d days.\n’’, name[n], daytab[leap][n]); } /* file3.c */ #include <stdlib.h> /* for atoi */ extern void set_leap(int); extern void print_days(int); int main(int argc, char *argv) { set_leap(atoi(argv[1])); print_days(atoi(argv[2])); return 0; } CSC180 Fall 2008, University of Toronto 3 • One-step compilation (generating a.out ): gcc file1.c file2.c file3.c • Separate compilation (also generating a.out ): gcc -c file1.c gcc -c file2.c gcc -c file3.c gcc file1.o file2.o file3.o • In the above sequence of commands, the first three will generate object files file1.o , file2.o , and file3.o , respectively. The last command links the three object files together, along with necessary library object files, to produce an executable a.out . CSC180 Fall 2008, University of Toronto 4 • If I only changed file3.c , I only need to recompile file3.c , and then link the updated file3.o with other unchanged object files. Thus, instead of using the command gcc file1.c file2.c file3.c , which compiles file1.c , file2.c , and file3.c again, I use the following commands: gcc -c file3.c gcc file3.o file1.o file2.o • Note that compilation (converting C source code to object code) takes considerable time, especially when we have many large C source files....
View Full Document

{[ snackBarMessage ]}

Page1 / 27

LEC20080929 - Introduction to Computer Programming Required...

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

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