comp26120 algorithms and imperative programming
play

COMP26120: Algorithms and Imperative Programming Lecture C5: C - - PowerPoint PPT Presentation

COMP26120: Algorithms and Imperative Programming Lecture C5: C - You Asked For It, You Got It Pete Jinks School of Computer Science, University of Manchester Autumn 2010 COMP26120 Lecture C5 1/34 Lewis Carroll The Hunting of the Snark


  1. COMP26120: Algorithms and Imperative Programming Lecture C5: C - You Asked For It, You Got It Pete Jinks School of Computer Science, University of Manchester Autumn 2010 COMP26120 Lecture C5 1/34

  2. Lewis Carroll – The Hunting of the Snark (an Agony, in Eight Fits) Fit the First – The Landing. ”Just the place for a Snark!” the Bellman cried, As he landed his crew with care; Supporting each man on the top of the tide By a finger entwined in his hair. ”Just the place for a Snark! I have said it twice: That alone should encourage the crew. Just the place for a Snark! I have said it thrice: What I tell you three times is true .” COMP26120 Lecture C5 3/34

  3. C.A.R.Hoare – 1980 ACM Turing Award Lecture. ”I was eventually persuaded of the need to design programming notations so as to maximise the number of errors which cannot be made, or if made, can be reliably detected at compile time. Perhaps this would make the text of programs longer. Never mind! Wouldn’t you be delighted if your Fairy Godmother offered to wave her wand over your program to remove all its errors and only made the condition that you should write out and key in your whole program three times !” COMP26120 Lecture C5 5/34

  4. Review ? COMP26120 Lecture C5 6/34

  5. Lecture Outline Language Design Philosophy YAFIYGI COMP26120 Lecture C5 7/34

  6. Lecture C5: You are here Language Design Philosophy YAFIYGI COMP26120 Lecture C5 Language Design Philosophy 8/34

  7. Design Goals of Java The Java Language Environment – May 1996 Simple, Object Oriented, and Familiar Robust and Secure Architecture Neutral and Portable High Performance Interpreted, Threaded, and Dynamic Extensive compile-time + run-time checking Strong typing – mainly Static Eliminate dangerous language features: automatic coercions, pointers, gotos, . . . and redundant language features: typedef, struct, union, . . . “Sacrifice code speed to get coding speed” COMP26120 Lecture C5 Language Design Philosophy 10/34

  8. Design Goals of Java The Java Language Environment – May 1996 Simple, Object Oriented, and Familiar Robust and Secure Architecture Neutral and Portable High Performance Interpreted, Threaded, and Dynamic Extensive compile-time + run-time checking Strong typing – mainly Static Eliminate dangerous language features: automatic coercions, pointers, gotos, . . . and redundant language features: typedef, struct, union, . . . “Sacrifice code speed to get coding speed” COMP26120 Lecture C5 Language Design Philosophy 10/34

  9. Design Goals of Java The Java Language Environment – May 1996 Simple, Object Oriented, and Familiar Robust and Secure Architecture Neutral and Portable High Performance Interpreted, Threaded, and Dynamic Extensive compile-time + run-time checking Strong typing – mainly Static Eliminate dangerous language features: automatic coercions, pointers, gotos, . . . and redundant language features: typedef, struct, union, . . . “Sacrifice code speed to get coding speed” COMP26120 Lecture C5 Language Design Philosophy 10/34

  10. C A low-level language for small computers e.g. operating systems, embedded systems “Don’t hide the hardware” Speed, Efficiency >> Safety A small language The programmer is knowledgeable; trust the programmer. Strengths: Efficiency, Power, Flexibility, Standard Library, Unix Weaknesses: Error-prone, Difficult to understand COMP26120 Lecture C5 Language Design Philosophy 12/34

  11. C A low-level language for small computers e.g. operating systems, embedded systems “Don’t hide the hardware” Speed, Efficiency >> Safety A small language The programmer is knowledgeable; trust the programmer. Strengths: Efficiency, Power, Flexibility, Standard Library, Unix Weaknesses: Error-prone, Difficult to understand COMP26120 Lecture C5 Language Design Philosophy 12/34

  12. C A low-level language for small computers e.g. operating systems, embedded systems “Don’t hide the hardware” Speed, Efficiency >> Safety A small language The programmer is knowledgeable; trust the programmer. Strengths: Efficiency, Power, Flexibility, Standard Library, Unix Weaknesses: Error-prone, Difficult to understand COMP26120 Lecture C5 Language Design Philosophy 12/34

  13. C Portability Unportable: – undefined (incorrect and no default behaviour) e.g. int overflow – implementation-defined (correct but compiler-dependant) e.g. int >> – unspecified (correct but no default behaviour) e.g. argument evaluation order Portable: – conforming (always get sensible answers) e.g. limits.h defines INT MAX etc. – strictly-conforming (always get same answers) e.g. stdint.h defines int32 t INT32 MAX etc. COMP26120 Lecture C5 Language Design Philosophy 14/34

  14. C Portability Unportable: – undefined (incorrect and no default behaviour) e.g. int overflow – implementation-defined (correct but compiler-dependant) e.g. int >> – unspecified (correct but no default behaviour) e.g. argument evaluation order Portable: – conforming (always get sensible answers) e.g. limits.h defines INT MAX etc. – strictly-conforming (always get same answers) e.g. stdint.h defines int32 t INT32 MAX etc. COMP26120 Lecture C5 Language Design Philosophy 14/34

  15. Lecture C5: You are here Language Design Philosophy YAFIYGI COMP26120 Lecture C5 YAFIYGI 15/34

  16. Weak Typing union oops { int a; float b; void * c; } ; (struct person*) malloc (...) Implicit Coercion; Explicit Cast Change bits; Change type but not bits COMP26120 Lecture C5 YAFIYGI 17/34

  17. Weak Typing union oops { int a; float b; void * c; } ; (struct person*) malloc (...) Implicit Coercion; Explicit Cast Change bits; Change type but not bits COMP26120 Lecture C5 YAFIYGI 17/34

  18. Weak Typing union oops { int a; float b; void * c; } ; (struct person*) malloc (...) Implicit Coercion; Explicit Cast Change bits; Change type but not bits COMP26120 Lecture C5 YAFIYGI 17/34

  19. to break or not to break switch(x) { default: if (prime(x)) case 2: case 3: case 5: case 7: process prime(x); else case 4: case 6: case 8: case 9: case 10: process non prime(x); } COMP26120 Lecture C5 YAFIYGI 19/34

  20. to break or not to break switch(x) { default: if (prime(x)) case 2: case 3: case 5: case 7: process prime(x); else case 4: case 6: case 8: case 9: case 10: process non prime(x); } COMP26120 Lecture C5 YAFIYGI 19/34

  21. to break or not to break switch(x) { default: if (prime(x)) case 2: case 3: case 5: case 7: process prime(x); else case 4: case 6: case 8: case 9: case 10: process non prime(x); } COMP26120 Lecture C5 YAFIYGI 19/34

  22. to break or not to break switch(x) { default: if (prime(x)) case 2: case 3: case 5: case 7: process prime(x); else case 4: case 6: case 8: case 9: case 10: process non prime(x); } COMP26120 Lecture C5 YAFIYGI 19/34

  23. , is an operator for (i=0, j=2; i<j; i+=2, j++) ... a, b= b, a; // swap a and b COMP26120 Lecture C5 YAFIYGI 21/34

  24. , is an operator for (i=0, j=2; i<j; i+=2, j++) ... a, b= b, a; // swap a and b COMP26120 Lecture C5 YAFIYGI 21/34

  25. = is an operator e.g. char *s, *t; while (*s++); while (*s++ == *t++); while (*s++ = *t++); COMP26120 Lecture C5 YAFIYGI 23/34

  26. = is an operator e.g. char *s, *t; while (*s++); while (*s++ == *t++); while (*s++ = *t++); COMP26120 Lecture C5 YAFIYGI 23/34

  27. = is an operator e.g. char *s, *t; while (*s++); while (*s++ == *t++); while (*s++ = *t++); COMP26120 Lecture C5 YAFIYGI 23/34

  28. boolean? int min=0, max=100, x; . . . min <= x <= max COMP26120 Lecture C5 YAFIYGI 25/34

  29. char? ’A’ getchar() COMP26120 Lecture C5 YAFIYGI 27/34

  30. Operator overloading (b) - (c); (b) + (c); (b) * (c); (b) & (c); (b) (c); COMP26120 Lecture C5 YAFIYGI 29/34

  31. Operator overloading (b) - (c); (b) + (c); (b) * (c); (b) & (c); (b) (c); COMP26120 Lecture C5 YAFIYGI 29/34

  32. Operator overloading (b) - (c); (b) + (c); (b) * (c); (b) & (c); (b) (c); COMP26120 Lecture C5 YAFIYGI 29/34

  33. Operator overloading (b) - (c); (b) + (c); (b) * (c); (b) & (c); (b) (c); COMP26120 Lecture C5 YAFIYGI 29/34

  34. White space a+++++b; char *s[]= { "A", "B", "C" "D", "E", } ; COMP26120 Lecture C5 YAFIYGI 31/34

  35. White space a+++++b; char *s[]= { "A", "B", "C" "D", "E", } ; COMP26120 Lecture C5 YAFIYGI 31/34

  36. Synonyms – “Syntactic Sugar” a=a+1; a+=1; a++; ++a; (*a).b; a->b; a[3]; *(a+3); *(3+a); 3[a]; for (A; B; C) { D; } A; while(B) { D; C; } COMP26120 Lecture C5 YAFIYGI 33/34

  37. Synonyms – “Syntactic Sugar” a=a+1; a+=1; a++; ++a; (*a).b; a->b; a[3]; *(a+3); *(3+a); 3[a]; for (A; B; C) { D; } A; while(B) { D; C; } COMP26120 Lecture C5 YAFIYGI 33/34

  38. Synonyms – “Syntactic Sugar” a=a+1; a+=1; a++; ++a; (*a).b; a->b; a[3]; *(a+3); *(3+a); 3[a]; for (A; B; C) { D; } A; while(B) { D; C; } COMP26120 Lecture C5 YAFIYGI 33/34

  39. Synonyms – “Syntactic Sugar” a=a+1; a+=1; a++; ++a; (*a).b; a->b; a[3]; *(a+3); *(3+a); 3[a]; for (A; B; C) { D; } A; while(B) { D; C; } COMP26120 Lecture C5 YAFIYGI 33/34

  40. Lecture Review Language Design Philosophy YAFIYGI COMP26120 Lecture C5 YAFIYGI 34/34

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend