Splitting your C program
When you separate your C program into many files, keep these points in
mind:
-
Be sure no two files have functions with the same name in it. The compiler
will get confused.
-
Similarly, if you use global variables in your program, be sure no two
files define the same global variables.
-
If you use global variables, be sure only one of the files defines
them, and declare them in your .h as follows:
extern int globalvar;
-
To use functions from another file, make a .h file with the function
prototypes, and use #include to include those .h files
within your .c files.
-
At least one of the files must have a main() function.
Note: When you define a variable, it looks like this: int
globalvar;. When you declare a variable, it looks like this:
extern
int globalvar;. The main difference is that a variable definition
creates the variable, while a declaration indicates that the variable is
defined elsewhere. A definition implies a declaration.
PreviousNextIndex
Last updated on Wednesday, June 21, 1995 by
Ben
Y. Yoshino
Copyright © 1995 University of Hawai`i, College of Engineering,
Computer Facility
All rights reserved.