Download presentation
Presentation is loading. Please wait.
1
Overview C and Data Structures Baojian Hua bjhua@ustc.edu.cn
2
What is this course about? Learn key elements of C language C is the most widely-used system language Linux, Windows, network server, compiler, … C is low-level Device driver, embedded (real-time) system, … The interplay with architecture Motivate ideas for other languages Especially the C++/Java/C# etc.
3
What is this course about? Understand elementary data structures Topics include: List, array, stack, queue, tree, graph, hash, sorting, searching, etc. General principals in designing and understanding data structures Tradeoffs between space and time, efficiency and clarity, etc. Design new data structures
4
What ’ s the Goal of this Course? Knowledge preparation C and data structures are crucial preliminaries for later computer courses Familiarize you with computer thinking Ex: abstraction, recursion Improve your programming skills More experience in programming Emphasis on modularity, ADT and clarity
5
Get Help Instructor: Hua, Baojian 87161325 bjhua@ustc.edu.cn 307, Mingde buiding Office hour: at every class, or to appoint TA: Xi, Jing 68839200 jingxi@ustc.edu.cn 234, Mingde building Office hour: at every class
6
Course Page Home page http://ssg.ustcsz.edu.cn/~hbj/summer08 http://ssg.ustcsz.edu.cn/~hbj/summer08 Course administrative Lecture notes Programming assignments Softwares Test and evaluation Check that page frequently
7
Textbooks and References The C Programming Language (Second Edition). Kernighan and Ritchie, 1988. C: A Reference Manual, Harbison and Steele, 2002. The Practice of Programming, Kernighan and Pike, 1999. Programming with GNU Software, Loukides and Oram, 1997. Fundamentals of data structures in C. Ellis Horowitz et al. 2006. Algorithm in C. Rdbert Sedgewick. Addison-Wesley Professional; 3 edition. 1997.
8
Homework There are 9 programming assignments plus some write-on-paper homework Optional assignments are not required, but highly recommended Solve them independently Late homework should only be considered under extraordinary circumstances Submit to Xi, Jing (jingxi@ustc.edu.cn)
9
Test and Evaluation There is a final test: Close book Cover all materials in the course Evaluation: 30% homework + 70% test Be concerned this course is more profitable and illuminating (and exciting) than you may assume
10
Any questions before we start?
11
The C Programming Language C is system programming language Originally used to write Unix Data types and control structures close to most machines Grow into popular general-purpose language Pros and cons: Can do whatever you want: flexible and powerful Can do whatever you do NOT want: shoot yourself in the foot History: BCPL B C K&R C ANSI C C99 1960 1970 1972 1978 1988 LISP Smalltalk C++ Java
12
First Program /* compile command: gcc –ansi first.c */ #include int main () { printf(“hello, world\n”); return 0; } main() is the program entry point, so every program must have a main() somewhere every function begins with {, and ends with } every statement terminated with ; we need the function printf to complete the task function argument, \n is newline return statement. the convention for main: 0 for normal return 1 for error case Comments. Has nothing to do with program run, but for documentation purpose.
13
Variables and Arithmetic Expressions Print the table of Fahrenheit temperatures and their centigrade or Celsius equivalents: C=(5/9)*(F-32) F C: 0 -17 20 -6 40 4 60 15 80 26 100 37 12048 140 60 160 71 180 82 200 93
14
Program #include int main() { int f, c; int lower, upper, step; lower = 0; upper = 200; step = 20; f = lower; while (f <= upper) { c = 5 * (f-32) / 9; printf("%d\t%d\n", f, c); f = f + step; } return 0; } F C: 0 -17 20 -6 40 4 60 15 80 26 100 37 12048 140 60 160 71 180 82 200 93
15
Data Types In C, the key word “ int ” stands for integer types Its range depends on the machine, typical 16 or 32 bits C provides other data types: charsingle character ‘ a ’, ‘ 8 ’, … longlong integers333L, … float32 bits3.14f, … doublefloat numbers3.14, …
16
Assignment Assignment statement: x=e; x is a variable, and e is an expression Meaning: set x the value of e Ex: lower = 0; upper = 200; c = 5 * (f - 32) / 9;
17
while while (f <= upper) {... } Meaning: if true then enter the loop, execute the statements; and back to the loop condition if false, then skip the loop body
18
while while (f <= upper) { //stms; } f<=upper // stms; YN the condition f<=upper is tested if true, then enter loop execute loop body, then go back to loop test if false; skip the loop
19
More on printf printf ("%d\t%d\n", f, c); Format string: Control how the data to output %d means output an integer See the text for a complete list and their meanings Escape character: ‘ \t ’, ‘ \n ’ more on this later
20
For Statement #include int main() { int f; for (f = 0; f <= 200; f = f + 20) { printf("%d\t%d\n", f, 5*(f-32)/9); } return 0; } start from herethen this f=0; f<=200 f=f+20; printf( … );
21
More on Function A function in C, is just like: subroutine in Fortran procedure or function in Pascal methods in Java A function provides convenient way to: encapsulate information modularize system (along with others) reuse code
22
Example // calculate circle area, #1 try #include int main() { float area; int r; r = 5; area = 3.14 * r * r; printf (“%f\n”, area); return 0; }
23
Or #include int main() { float area; int r; for (r = 0; r<10; r++) { area = 3.14 * r * r; printf (“%f\n”, area); } return 0; }
24
Code Duplication // calculate circle area, 1 st try int main() { float area; int r; for (r = 0; r<10; r++) area = 3.14 * r * r; // hard to modify, say, to 3.14159 for (r = 10; r<20; r++) area = 3.14 * r * r; return 0; }
25
Using Function float area(int r); float area(int r) { float pi = 3.14; return (pi * r * r); } int main() { float a; int r; for (r=0; r<10; r++) a = area(r); return 0; } function prototype function definition function body, as we have discussed for main() implicit conversion function call with argument r
26
Function Summary Function prototype: return-type function-name (parameter declarations, if any) ; Function definition: return-type function-name (parameter declarations, if any) { declarations statements } Function call: function-name (expressions, if any);
27
Call-by-value void foo (int n); void foo(int n) { n--; return; } int main() { int n = 9; foo (n); printf(“%d\n”, n); return 0; } decrement operation, n=n-1 type void, no value the value of n? 9 9 8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.