Presentation is loading. Please wait.

Presentation is loading. Please wait.

C and Data Structures Baojian Hua

Similar presentations


Presentation on theme: "C and Data Structures Baojian Hua"— Presentation transcript:

1 C and Data Structures Baojian Hua bjhua@ustc.edu.cn
Functions C and Data Structures Baojian Hua

2 Typical C Program Organization
file1 filen function1 functionm function1 functionn

3 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;

4 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 dec (int a) i = a;

5 External Variables variable definitions // file1.c int i; float j[10];
int f() { } variable declarations // file2.c extern int i; extern float j[]; int g() { i = 9; }

6 External Variables Pros: Cons: An important way for data sharing
The poorest way 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 So, it should be used rarely, if at all

7 Scope Rules automatic (local) variable
from the declaration point to block end extern variables; function names from the declaration point to file end “extern” extern variables in other files

8 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[];

9 Static Variables By default, all extern variables and functions (in all 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 visa number and passwd C provides the “static” mechanism

10 Static Variables // visa.c int myDollar; int lookup (){
return myDollar; } void add (int dollar){ myDollar += dollar; void sub (int dollar){ myDollar -= dollar; // main.c extern myDollar; extern void sub (int dollar); int main () { myDollar -= ; sub (999999); }

11 Static Variables // visa.c static int myDollar; int lookup (){
return myDollar; } void add (int dollar){ myDollar += dollar; static void sub (int dollar){ myDollar -= dollar; // main.c extern myDollar; extern void sub (int dollar); int main () { myDollar -= ; sub (999999); }

12 Static Variables “Static” can also applied to automatic variables
to tie different function calls Overall, the terminology “static” is a little misleading maybe “private” is more meaningful

13 Initialization variable sorts automatic init’ user init’ auto NO
any expression extern variable YES. init’ to 0 constants “extern” Forbidden

14 Header Files 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 loading libraries Cons: flat name space

15 Header Files Example // area.h // area.c #include “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; }

16 Or Extern Variable // pi.c double pi=3.14; // area.h
double area (int r); // area.c #include “area.h” extern double pi; double area (int r){ double f = pi *r *r; return f; } // main.c #include “area.h” int main (){ double f; f = area (5); return 0; }

17 Problems // area.h #include “pi.h” double area (int r); // pi.h
double pi; // main.c #include “area.h” #include “pi.h” int main (){ double f; f = area (5); return 0; } There will be errors, if some declaration appear more than once (say, structure declarations).

18 Conditional Inclusion
// area.h #ifndef AREA_H #define AREA_H #include “pi.h” double area (int r); #endif // pi.h #ifndef PI_H #define PI_H double pi; #endif // main.c #include “area.h” #include “pi.h”

19 Recursive Functions // Consider the fibnacci numbers:
// { 0, if n = 0; // fib n = { 1, if n = 1; // { fib(n-1)+ fib(n-2), otherwise. // The C code is: int fib (int n) { if (0==n) return 0; else if (1==n) return 1; else return (fib(n-1) + fib(n-2)); }

20 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) // = fib(1)+fib(2)+fib(3) // = fib(2)+fib(3) // = … = 5 // As we can see, it’s too inefficient as we are // too stupid doing much redundant computations. // Exercise: is there a more efficient version?

21 Some More Macros


Download ppt "C and Data Structures Baojian Hua"

Similar presentations


Ads by Google