200 likes | 206 Views
This text explores the organization of C programs, function definitions, external variables, static variables, header files, and conditional inclusion. Includes examples and explanations.
E N D
Functions C and Data Structures Baojian Hua bjhua@ustc.edu.cn
Typical C Program Organization Program … file1 filen … … function1 functionm function1 functionn
Function Definition return-type function-name (argument declarations) { declarations and statements } // example: int sum (int a, int b) { int temp; temp = a + b; return temp; }
External Variables // Instead of function arguments and local // variables, we may also declare external // variables. External: not within any function. int i; int get () { return i; } void set (int a) { i = a; }
Scope Rule Example int a; // #2 extern int b; // #3 void foo () { int i; // #1 while (…) { int j; // #1 b = i+j; } extern void bar (); // #3 …; bar (); }
variable definitions variable declarations External Variables // file1.c int i; float j[10]; int f () { …; } // file2.c extern int i; extern float j[]; int g () { i = 9; …; }
Declarations and Definitions // file3.c extern int i; extern float j[]; … // file1.c int i; float j[10]; int f() { … } // file2.c extern int i; extern float j[]; …
External Variables • Pros: • An important way for data sharing • The poor man’s method to build closures • In future slides, we’ll discuss another one • Also think objects in OO languages • Cons: • External variables blur the connections between functions and modules • Involve the internal working of a linker
Static Variables • By default, all external variables and functions (in all source files) are visible to all program code • whether or not in same source file • However, in some circumstance, we want to keep our data private • Ex: visa number and passwd • C provides the “static” mechanism
Static Variables // visa.c int myDollar; int lookup () { return myDollar; } void add (int a) { myDollar += a; } // void sub (int a) // main.c extern int myDollar; extern void add (int a); int main () { // Ooooops! myDollar -= 999999; add (999999); …; }
Static Variables // visa.c static int myDollar; int lookup () { return myDollar; } void add (int a) { myDollar += a; } // void sub (int a) // main.c // compiler complains… extern int myDollar; extern void add (int a); int main () { // Ooooops! myDollar -= 999999; add (999999); …; }
Static Variables • “Static” can also applied to automatic variables • to tie different function calls • In a summary, the terminology “static” is a little misleading • maybe “private” is more meaningful, just as that of C++ or Java
Header Files • Problem with “extern”? • A header file • group common declarations together • could be included by other files • typically named *.h • Header file is C’s rudimentary module system • Pros: • Separate compilation • Essential for linking user code with libraries • Cons: • flat name space
Header Files Example // area.h double area (int r); // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; } // main.c #include “area.h” int main () { double f; f = area (5); return 0; }
Header Files Example // area.h double area (int r); // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; } // main.c #include “area.h” int main () { double f; f = area (5); return 0; }
Conditional Inclusion // area.h #ifndef AREA_H #define AREA_H double area (int r); #endif // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; } Why these? We’d discuss it in future slides. // main.c #include “area.h” …
Recursive Functions // Consider the fibnacci numbers: // { 0, if n == 0; // fib (n) = { 1, if n == 1; // { fib(n-1) + fib(n-2), otherwise. int fib (int n) { switch (n) { case 0: return 0; case 1: return 1; default: return fib(n-1) + fib(n-2); } }
Recursive Functions // Next, we crawl through this function to see // how fib(5) is computed: fib (5) = fib(4)+fib(3) = fib(3)+fib(2)+fib(3) = fib(2)+fib(1)+fib(2)+fib(3) = fib(1)+fib(0)+fib(1)+fib(2)+fib(3) = 1 + 0 + fib(1)+fib(2)+fib(3) = 1 + 1 + fib(2)+fib(3) = … = 5 // As we can see, it’s too inefficient as we are // too stupid doing much redundant computations. Exercise: design a more efficient version!