Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Fundamentals CGS 3460, Lecture 4 Jan 18, 2006 Hen-I Yang.

Similar presentations


Presentation on theme: "C Fundamentals CGS 3460, Lecture 4 Jan 18, 2006 Hen-I Yang."— Presentation transcript:

1 C Fundamentals CGS 3460, Lecture 4 Jan 18, 2006 Hen-I Yang

2 Previously… Machine Code – Assembly – C 3 Steps to a runnable program. + Demo Greetings.c Software Engineering: Implementing a software that solves real problems. Importance of commenting

3 Administrivia Introducing your TAs. TAs Office Hours (E309)  Gang Liugaliu@cise.ufl.edugaliu@cise.ufl.edu Tu 6 – 9th periods  Zhen Yangzhyang@cise.ufl.eduzhyang@cise.ufl.edu M 5—7th, Th 3 – 5th periods  Xiao Li xl1@cise.ufl.eduxl1@cise.ufl.edu W 7—8 th, F 5—7th periods Info available on the web site. A new Q&A page has been added.

4 Administrivia (II) Put [CGS3460] in the subject when sending emails related to the course. Email/questions asked during office hours regarding course related materials may be edited, compiled or modified and posted on the web to help other students learn. The compilation and edition will protect students’ privacy. If you strongly against the discussion being edited, modified or posted, please indicate clearly [DO NOT POST] in your email.

5 Agenda Continue Discussion on “Software Engineering: Implementing a software that solves the problem”. Discuss Vending Machine Exercise.

6 Cold Pizza Software Engineering: Implementing a software that solves the problem. Importance of commenting.

7 From Analysis to Executable Problem Analysis (Human Perception) Strategy Forming (Pesudo code) Implementation(High Level Language) Compilation(Assembly Language) Debug Execution(Machine Code) Debug/Testing Example: Greeting Program.

8 Problem: Write a program that greets the user Step 1: Collect User’s Name Step 2: Print Greeting Message On Screen Name  Collect User’s Name Name  Screen Greetings

9 #include /** * First sample program of CGS 3460, spring 2006 * Author: Hen-I Yang * Date: Jan 10, 2006 * Version: 1.0 * Note: This program greets the user * Revision: **/ int main() { /* Declare a variable called name to store name collected */ /* Allocate Memory to Record User’s Name Collected */ /* Collect User’s Name (Name  Collect User’s Name) */ /* Print a Greeting Message (Name  Screen) */ return 0; }

10 #include /** * First sample program of CGS 3460, spring 2006 * Author: Hen-I Yang * Date: Jan 10, 2006 * Version: 1.0 * Note: * Revision: **/ int main() { /* Declare a variable called name to store name collected */ char *name; /* Allocate Memory to Record User’s Name Collected */ name = (char *) malloc (25); /* Collect User’s Name (Name  Collect User’s Name) */ puts ("Please enter your name:"); scanf ("%20s", name); /* Print a Greeting Message (Name  Screen) */ printf ("Hello, %s\n", name); return 0; }

11 #include int main() { char *name; name = (char *) malloc (25); puts ("Please enter your name:"); scanf ("%20s", name); printf ("\nHello, %s\n", name); return 0; } C Source Code

12 106fc:9d e3 bf 88 save %sp, -120, %sp 10700:90 10 20 19 mov 0x19, %o0 10704:40 00 40 71 call 208c8 10708:01 00 00 00 nop 1070c:82 10 00 08 mov %o0, %g1 10710:c2 27 bf ec st %g1, [ %fp + -20 ] 10714:03 00 00 41 sethi %hi(0x10400), %g1 10718:90 10 63 f8 or %g1, 0x3f8, %o0! 107f8 1071c:40 00 40 6e call 208d4 10720:01 00 00 00 nop 10724:03 00 00 42 sethi %hi(0x10800), %g1 10728:90 10 60 10 or %g1, 0x10, %o0! 10810 1072c:d2 07 bf ec ld [ %fp + -20 ], %o1 10730:40 00 40 6c call 208e0 10734:01 00 00 00 nop 10738:03 00 00 42 sethi %hi(0x10800), %g1 1073c:90 10 60 18 or %g1, 0x18, %o0! 10818 10740:d2 07 bf ec ld [ %fp + -20 ], %o1 10744:40 00 40 6a call 208ec 10748:01 00 00 00 nop 1074c:82 10 20 00 clr %g1! 0 10750:b0 10 00 01 mov %g1, %i0 10754:81 c7 e0 08 ret 10758:81 e8 00 00 restore 1075c:81 c3 e0 08 retl 10760:ae 03 c0 17 add %o7, %l7, %l7 Assembly

13 106fc:9d e3 bf 88 save %sp, -120, %sp 10700:90 10 20 19 mov 0x19, %o0 10704:40 00 40 71 call 208c8 10708:01 00 00 00 nop 1070c:82 10 00 08 mov %o0, %g1 10710:c2 27 bf ec st %g1, [ %fp + -20 ] 10714:03 00 00 41 sethi %hi(0x10400), %g1 10718:90 10 63 f8 or %g1, 0x3f8, %o0! 107f8 1071c:40 00 40 6e call 208d4 10720:01 00 00 00 nop 10724:03 00 00 42 sethi %hi(0x10800), %g1 10728:90 10 60 10 or %g1, 0x10, %o0! 10810 1072c:d2 07 bf ec ld [ %fp + -20 ], %o1 10730:40 00 40 6c call 208e0 10734:01 00 00 00 nop 10738:03 00 00 42 sethi %hi(0x10800), %g1 1073c:90 10 60 18 or %g1, 0x18, %o0! 10818 10740:d2 07 bf ec ld [ %fp + -20 ], %o1 10744:40 00 40 6a call 208ec 10748:01 00 00 00 nop 1074c:82 10 20 00 clr %g1! 0 10750:b0 10 00 01 mov %g1, %i0 10754:81 c7 e0 08 ret 10758:81 e8 00 00 restore 1075c:81 c3 e0 08 retl 10760:ae 03 c0 17 add %o7, %l7, %l7 Machine Code

14 Hex to Binary Conversion Problem: Given a number in hex(adecimal), convert it to binary representation Get a number in Hex, print out a number in binary Convert to Decimal Divide the number n by 2 k, (k = 3, 2, 1, 0)  if the quote is 1, put down 1, n = n – 2 k  If the quote is 0, put down 0  moves on to the next k

15 Gatorade Vending Machine Problem: Design a software that can handle transactions of vending machine Strategy: Input1: Nickle, Dime, Quarter (Penny not allowed) Input2: Orange, Blue, Green, Pink, Refund Output: Drink and Change keep receiving Input1 until Input2 is received if ΣInput1 > Input2 then return Input + (ΣInput1 - Input2) else return (“insufficient fund”  screen)

16 Comments on Comments Programs should be readable by both computer and human Remind yourself what the code does Extremely useful when debugging Important for collaboration Readability

17 History of C Language Developed in early 1970s. By product of UNIX operating system. (DEC PDP-7) Predecessor: Algol, B, NB 1978, The C Programming Langauge (K&R) Widespread to IBM PC in 80s 1983, ANSI-C C++

18 Strength/Weakness of C Low-level language (Machine Level, computer’s built-in instruction and fast, Socket, etc) Permissive Flexibility Small, efficient, powerful language, execute and develop fast Standard Library Power Portability Integration with UNIX Too close to machine Error-prone Difficult to understand (many advanced features, terse nature, outdated with minimal interactive, programmers are smarter) Difficult to modify (no module, etc)

19 Why learn C instead of C++ C++ is much harder to learn Still lots of C code around Not everyone is likely to switch to C++ Still the lowest maintenance for small programs ----------- Side Effect -------------- avoid learning bad habits

20 Summary 7 steps from analysis of problem to executable. 3 examples of problem analysis Comments. C trivia. (Chapter 1)

21 Before you go Make sure you finish reading Chapter 2. Read Chapter 3. Exercises 2.11 and 2.12.


Download ppt "C Fundamentals CGS 3460, Lecture 4 Jan 18, 2006 Hen-I Yang."

Similar presentations


Ads by Google