Beruflich Dokumente
Kultur Dokumente
Programming Languages – C
Lecture 2
Matthew P. Johnson
Columbia University
Fall 2003
00000001 = 1
+ 10000001 = -1
------------
10000010 = -2
TRUE};
Req: names (but not vals) must be
unique
Legal:
year? year % 4 == 0
If 4 | year
Unless y % 100
Unless 100 |
== 0
year
Unless y % 400
Unless 400 |
year == 0
b1 && (!b2 || b3)
If the second is
main() {
double sq = square(4); /* poss parse error
reads 4 as dbl */
double sq = sqaure((double)4);
}
double sqaure(double d) {
return d*d;
}
10/17/08 CS3101-1, Lecture 2 34
Casting
General form: put desired type name in
parentheses, prepend to expr:
(new-type)expr;
(float)i;
Also useful in I/O
Saw (briefly) before: printf uses
pattern-matching for positioning:
printf(“val = %d\n”, 3);
printf(“val = %f\n”, 3.5);
output is 65
Do we need casting?
No. ‘A’ is “really” the value 65;
don’t want to convert the value
Want to change the presentation:
printf(“ch == %c\n”, ch);
Assign. ops
10/17/08 CS3101-1, Lecture 2 48
Order of eval
Most ops eval-ed L to R
Two things to know:
1. Short-circuiting
if (a==b && c==ftn()) …
a!=b c==ftn() is never eval-ed!
ftn() (maybe with side-effects?) is not called!
Useful for prevent null-ptr problems:
if (a != NULL && a[4] == 2) …
No error
2. Assign op eval-ed R-to-L:
a ^= b ^= a ^= b;
a = b = c = d = 0;
10/17/08 CS3101-1, Lecture 2 49
Flow control
Saw if, if-else
Suppose want to respond to many diff
poss vals:
if (day == MON)
…;
else if (day == TUE)
…;
else if … run-on if-else