COMP26120: Algorithms and Imperative...
Transcript of COMP26120: Algorithms and Imperative...
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
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
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
Review
?
COMP26120 Lecture C5 6/34
Lecture Outline
Language Design Philosophy
YAFIYGI
COMP26120 Lecture C5 7/34
Lecture C5: You are here
Language Design Philosophy
YAFIYGI
COMP26120 Lecture C5 Language Design Philosophy 8/34
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
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
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
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
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
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
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
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
Lecture C5: You are here
Language Design Philosophy
YAFIYGI
COMP26120 Lecture C5 YAFIYGI 15/34
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
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
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
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
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
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
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
, 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
, 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
= is an operator
e.g. char *s, *t;
while (*s++);
while (*s++ == *t++);
while (*s++ = *t++);
COMP26120 Lecture C5 YAFIYGI 23/34
= is an operator
e.g. char *s, *t;
while (*s++);
while (*s++ == *t++);
while (*s++ = *t++);
COMP26120 Lecture C5 YAFIYGI 23/34
= is an operator
e.g. char *s, *t;
while (*s++);
while (*s++ == *t++);
while (*s++ = *t++);
COMP26120 Lecture C5 YAFIYGI 23/34
boolean?
int min=0, max=100, x;. . .min <= x <= max
COMP26120 Lecture C5 YAFIYGI 25/34
char?
’A’
getchar()
COMP26120 Lecture C5 YAFIYGI 27/34
Operator overloading
(b) - (c);
(b) + (c);
(b) * (c);
(b) & (c);
(b) (c);
COMP26120 Lecture C5 YAFIYGI 29/34
Operator overloading
(b) - (c);
(b) + (c);
(b) * (c);
(b) & (c);
(b) (c);
COMP26120 Lecture C5 YAFIYGI 29/34
Operator overloading
(b) - (c);
(b) + (c);
(b) * (c);
(b) & (c);
(b) (c);
COMP26120 Lecture C5 YAFIYGI 29/34
Operator overloading
(b) - (c);
(b) + (c);
(b) * (c);
(b) & (c);
(b) (c);
COMP26120 Lecture C5 YAFIYGI 29/34
White space
a+++++b;
char *s[]= {"A", "B", "C" "D", "E",};
COMP26120 Lecture C5 YAFIYGI 31/34
White space
a+++++b;
char *s[]= {"A", "B", "C" "D", "E",};
COMP26120 Lecture C5 YAFIYGI 31/34
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
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
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
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
Lecture Review
Language Design Philosophy
YAFIYGI
COMP26120 Lecture C5 YAFIYGI 34/34