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

comp26120 algorithms and imperative programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 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

slide-3
SLIDE 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

  • nly made the condition that you should

write out and key in your whole program three times!”

COMP26120 Lecture C5 5/34

slide-4
SLIDE 4

Review

?

COMP26120 Lecture C5 6/34

slide-5
SLIDE 5

Lecture Outline

Language Design Philosophy YAFIYGI

COMP26120 Lecture C5 7/34

slide-6
SLIDE 6

Lecture C5: You are here

Language Design Philosophy YAFIYGI

COMP26120 Lecture C5 Language Design Philosophy 8/34

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 15

Lecture C5: You are here

Language Design Philosophy YAFIYGI

COMP26120 Lecture C5 YAFIYGI 15/34

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 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

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 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

slide-25
SLIDE 25

= is an operator

e.g. char *s, *t; while (*s++); while (*s++ == *t++); while (*s++ = *t++);

COMP26120 Lecture C5 YAFIYGI 23/34

slide-26
SLIDE 26

= is an operator

e.g. char *s, *t; while (*s++); while (*s++ == *t++); while (*s++ = *t++);

COMP26120 Lecture C5 YAFIYGI 23/34

slide-27
SLIDE 27

= is an operator

e.g. char *s, *t; while (*s++); while (*s++ == *t++); while (*s++ = *t++);

COMP26120 Lecture C5 YAFIYGI 23/34

slide-28
SLIDE 28

boolean?

int min=0, max=100, x; . . . min <= x <= max

COMP26120 Lecture C5 YAFIYGI 25/34

slide-29
SLIDE 29

char?

’A’ getchar()

COMP26120 Lecture C5 YAFIYGI 27/34

slide-30
SLIDE 30

Operator overloading

(b) - (c); (b) + (c); (b) * (c); (b) & (c); (b) (c);

COMP26120 Lecture C5 YAFIYGI 29/34

slide-31
SLIDE 31

Operator overloading

(b) - (c); (b) + (c); (b) * (c); (b) & (c); (b) (c);

COMP26120 Lecture C5 YAFIYGI 29/34

slide-32
SLIDE 32

Operator overloading

(b) - (c); (b) + (c); (b) * (c); (b) & (c); (b) (c);

COMP26120 Lecture C5 YAFIYGI 29/34

slide-33
SLIDE 33

Operator overloading

(b) - (c); (b) + (c); (b) * (c); (b) & (c); (b) (c);

COMP26120 Lecture C5 YAFIYGI 29/34

slide-34
SLIDE 34

White space

a+++++b; char *s[]= {"A", "B", "C" "D", "E",};

COMP26120 Lecture C5 YAFIYGI 31/34

slide-35
SLIDE 35

White space

a+++++b; char *s[]= {"A", "B", "C" "D", "E",};

COMP26120 Lecture C5 YAFIYGI 31/34

slide-36
SLIDE 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

slide-37
SLIDE 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

slide-38
SLIDE 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

slide-39
SLIDE 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

slide-40
SLIDE 40

Lecture Review

Language Design Philosophy YAFIYGI

COMP26120 Lecture C5 YAFIYGI 34/34