comp26120: algorithms and imperative...

40
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

Upload: others

Post on 16-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

COMP26120: Algorithms and ImperativeProgrammingLecture 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

Page 2: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Lewis Carroll – The Hunting of the Snark (an Agony, inEight 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 tideBy 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

Page 3: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

C.A.R.Hoare – 1980 ACM Turing Award Lecture.

”I was eventually persuaded of the need to designprogramming notations so as to maximise the number of errorswhich cannot be made, or if made, can be reliably detected atcompile time.

Perhaps this would make the text of programs longer. Nevermind!

Wouldn’t you be delighted if your Fairy Godmother offered towave her wand over your program to remove all its errors andonly made the condition that you shouldwrite out and key in your whole program three times!”

COMP26120 Lecture C5 5/34

Page 4: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Review

?

COMP26120 Lecture C5 6/34

Page 5: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Lecture Outline

Language Design Philosophy

YAFIYGI

COMP26120 Lecture C5 7/34

Page 6: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Lecture C5: You are here

Language Design Philosophy

YAFIYGI

COMP26120 Lecture C5 Language Design Philosophy 8/34

Page 7: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Design Goals of Java

The Java Language Environment – May 1996

Simple, Object Oriented, and FamiliarRobust and SecureArchitecture Neutral and PortableHigh PerformanceInterpreted, Threaded, and Dynamic

Extensive compile-time + run-time checkingStrong typing – mainly StaticEliminate 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

Page 8: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Design Goals of Java

The Java Language Environment – May 1996

Simple, Object Oriented, and FamiliarRobust and SecureArchitecture Neutral and PortableHigh PerformanceInterpreted, Threaded, and Dynamic

Extensive compile-time + run-time checkingStrong typing – mainly StaticEliminate 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

Page 9: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Design Goals of Java

The Java Language Environment – May 1996

Simple, Object Oriented, and FamiliarRobust and SecureArchitecture Neutral and PortableHigh PerformanceInterpreted, Threaded, and Dynamic

Extensive compile-time + run-time checkingStrong typing – mainly StaticEliminate 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

Page 10: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

C

A low-level language for small computerse.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

Page 11: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

C

A low-level language for small computerse.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

Page 12: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

C

A low-level language for small computerse.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

Page 13: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

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

Page 14: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

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

Page 15: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Lecture C5: You are here

Language Design Philosophy

YAFIYGI

COMP26120 Lecture C5 YAFIYGI 15/34

Page 16: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

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

Page 17: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

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

Page 18: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

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

Page 19: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

to break or not to break

switch(x) {default:if (prime(x))

case 2: case 3: case 5: case 7:process prime(x);

elsecase 4: case 6: case 8: case 9: case 10:

process non prime(x);}

COMP26120 Lecture C5 YAFIYGI 19/34

Page 20: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

to break or not to break

switch(x) {default:if (prime(x))

case 2: case 3: case 5: case 7:process prime(x);

elsecase 4: case 6: case 8: case 9: case 10:

process non prime(x);}

COMP26120 Lecture C5 YAFIYGI 19/34

Page 21: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

to break or not to break

switch(x) {default:if (prime(x))

case 2: case 3: case 5: case 7:process prime(x);

elsecase 4: case 6: case 8: case 9: case 10:

process non prime(x);}

COMP26120 Lecture C5 YAFIYGI 19/34

Page 22: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

to break or not to break

switch(x) {default:if (prime(x))

case 2: case 3: case 5: case 7:process prime(x);

elsecase 4: case 6: case 8: case 9: case 10:

process non prime(x);}

COMP26120 Lecture C5 YAFIYGI 19/34

Page 23: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

, 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

Page 24: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

, 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

Page 25: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

= is an operator

e.g. char *s, *t;

while (*s++);

while (*s++ == *t++);

while (*s++ = *t++);

COMP26120 Lecture C5 YAFIYGI 23/34

Page 26: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

= is an operator

e.g. char *s, *t;

while (*s++);

while (*s++ == *t++);

while (*s++ = *t++);

COMP26120 Lecture C5 YAFIYGI 23/34

Page 27: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

= is an operator

e.g. char *s, *t;

while (*s++);

while (*s++ == *t++);

while (*s++ = *t++);

COMP26120 Lecture C5 YAFIYGI 23/34

Page 28: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

boolean?

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

COMP26120 Lecture C5 YAFIYGI 25/34

Page 29: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

char?

’A’

getchar()

COMP26120 Lecture C5 YAFIYGI 27/34

Page 30: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Operator overloading

(b) - (c);

(b) + (c);

(b) * (c);

(b) & (c);

(b) (c);

COMP26120 Lecture C5 YAFIYGI 29/34

Page 31: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Operator overloading

(b) - (c);

(b) + (c);

(b) * (c);

(b) & (c);

(b) (c);

COMP26120 Lecture C5 YAFIYGI 29/34

Page 32: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Operator overloading

(b) - (c);

(b) + (c);

(b) * (c);

(b) & (c);

(b) (c);

COMP26120 Lecture C5 YAFIYGI 29/34

Page 33: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Operator overloading

(b) - (c);

(b) + (c);

(b) * (c);

(b) & (c);

(b) (c);

COMP26120 Lecture C5 YAFIYGI 29/34

Page 34: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

White space

a+++++b;

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

COMP26120 Lecture C5 YAFIYGI 31/34

Page 35: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

White space

a+++++b;

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

COMP26120 Lecture C5 YAFIYGI 31/34

Page 36: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

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

Page 37: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

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

Page 38: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

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

Page 39: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

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

Page 40: COMP26120: Algorithms and Imperative Programmingsyllabus.cs.manchester.ac.uk/ugt/2017/COMP26120/lecture/pjjhando… · Programming Lecture C5: C - You Asked For It, You Got It Pete

Lecture Review

Language Design Philosophy

YAFIYGI

COMP26120 Lecture C5 YAFIYGI 34/34