30
APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

APS105Winter 2012

Jonathan Deberjdeber -at- cs -dot- toronto -dot- edu

Lecture 4January 23, 2012

Page 2: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Today• Math

• Characters

• Constants

• Office Hours begin today @ 1:00 pm (BA 2270)

• Labs and Tutorials begin this week

2

Page 3: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

!"#$%&'(

• To use )*"#+,, need to tell compiler where to find it

• !"#$%&'(-./+'"0123 at beginning of file

• Tells C to use the stdio (standard I/O) library

3

Page 4: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

The Correct Program• No such thing as the correct program

• There are correct/incorrect answers

• There are better/worse programs

4

Page 5: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012
Page 6: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Source: http://us.battle.net/sc2/

Page 7: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Math

7

Page 8: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Addition and Subtraction• Mostly what you expect

• Exception is overflow/underflow

"#+-"-4-56"#+-7-4-86

"#+-9-4-"-:-769-4-"-;-76

'0&<%(-'-4-51=6'0&<%(-(-4-81>6

'0&<%(-,-4-'-:-(6,-4-'-;-(6

8

Page 9: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Multiplication• Mostly what you expect (except we use ?)

• Exception is overflow/underflow

"#+-"-4-56"#+-7-4-86

"#+-9-4-"-?-76

'0&<%(-'-4-51=6'0&<%(-(-4-81>6

'0&<%(-,-4-'-?-(6

9

Page 10: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Division• Uses /, since there is no ÷ key

• '0&<%(

• What you expect

• "#+

• floor division

• @ (modulo or mod)

'0&<%(-'-4->1=6'0&<%(-(-4-51=6

'0&<%(-,-4-'-A-(6

"#+-"-4->6"#+-7-4-56

"#+-9-4-"-A-76"#+-%-4-"-@-76

51>

5

B10

Page 11: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Division

C-is not-DC-A-EF-?-E

11

x =x

yy

C-is DC-A-EF-?-E-:-DC-@-EF

Page 12: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Division• Negative numbers are tricky

• "#+-"-4-;>-A-56

• Mathematically, that’s -2.5

• But, we can round that to either -2 or -3

• C89 is implementation dependent

• C99 says truncate towards 0

12

Page 13: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Order of Operations• What you expect (for basic math)

• unary : and ;, ? and A, : and ;

• ;5-?-8-:->

• You can use parentheses, even when not required

• ;5-?-D8-:->F

• D;5-?-8F-:->

;G

;BH

;G

13

Page 14: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Associativity• B5-A-8-?-I

• Is that DB5-A-8F-?-I or B5-A-D8-?-IF?

• Arithmetic operators are left associative

-8-?-G-:-8-A-I-?-8-:-J-A-GD8-?-GF-:-8-A-I-?-8-:-J-A-GD8-?-GF-:-D8-A-IF-?-8-:-J-A-GD8-?-GF-:-DD8-A-IF-?-8F-:-J-A-GD8-?-GF-:-DD8-A-IF-?-8F-:-DJ-A-GF

14

Page 15: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Short Forms#&KL+(K/-4-#&KL+(K/-:-B6#&KL+(K/-:4-B6#&KL+(K/::6

M("N2+-;4-G6*O+"0-A4-81J6O'7&/+('-?4-86

M("N2+-4-M("N2+-;-G6

::#&KL+(K/6

*O+"0-4-*O+"0-A-81J6O'7&/+('-4-O'7&/+('-?-86

#&KL+(K/-4:-56 #&KL+(K/-4-D:5F6

15

Wrong

Page 16: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Mixed Types• C will (mostly) convert for you

• Arithmetic conversion:

• "#+ ! '0&<%( as necessary

• Little to no loss of data

• Assignment conversion:

• "#+ ! '0&<%( as before

• '0&<%( ! "#+ is truncated

"#+-"-4-56'0&<%(-'-4-51>6'0&<%(-(-4-"-:-'6

51=-:-51>

"#+-"-4-G1B8B>J6G

16

'0&<%(-'-4-G6G1=

Page 17: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Converting Types• 5AG-?-J

• 51=AG-?-J

• '0&<%(-'-4-5AG-?-J6

17

=1=

=

I1=

Page 18: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Casting• Manually convert an expression from one type to another

• D'0&<%(F5 ! 51=

• '0&<%(-'-4-5AG-?-J6

• '0&<%(-(-4-D'0&<%(F5AG-?-J6

• '0&<%(-,-4-5AD'0&<%(FG-?-J6

• '0&<%(-N-4-5AG-?-D'0&<%(FJ6

• '0&<%(-2-4-D'0&<%(FD5AG-?-JF618

=1=

I1=

I1=

=1=

=1=

Page 19: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Casting

19

'0&<%(-(-4-D'0&<%(F5AG-?-J6'0&<%(-,-4-51=AG1=-?-J1=6

"#+-#-4-B=6"#+-/&K-4-B=>6'0&<%(-O%%0$O+"0#-4-/&K-A-#6O%%0$O+"0#-4-D'0&<%(F/&K-A-#6

B=1=

B=1>

Page 20: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Characters

20

Page 21: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Characters• Smallest unit of text

• POP

• -P>P

• -PQ#P

• Type is $2O*

• Strings are sequences of characters

• RS"R is PSP-followed by-P"P

• ROR vs. POP

Pronunciation:

$2O*-O$+(*6

$2O*-<*0"%('6

21

Page 22: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Characters• Need to store (encode) them as Bs and =s

• Could decide something like O = B, < = 5, $ = G, etc.

• ASCII

• 128 characters = 27 values = 7 bits

• Stored in 8 bits = 1 byte

• Some logic, some arbitrariness

• Only represents what’s on an English keyboard

22

Page 23: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

ASCII• Control characters (0-31)

• Digits (48-57)

• Upper case (65-90)

• Lower case (97-122)

• Basic symbols (everything else)

23

Page 24: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Characters• Escape sequences

• PQ#P or PQRP

• PQ=P is character code =

• “Null character”

• PQ=P vs. P=P

24

Page 25: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

)*"#+,

• Uses @$ format specifier

25

$2O*-,"*/+-4-PTP6)*"#+,DRUE-#OK(-/+O*+/-M"+2V-@$1-Q#RW-,"*/+F6

UE-#OK(-/+O*+/-M"+2V-T1

Page 26: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

$2O*-$-4-POP6$::6$::6

$-;4-G56

P<P

P$P

PXP

• Generally, not a great idea

• We’ll see better alternatives later

Treating $2O*/ as Numbers

26

Page 27: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Constants

27

Page 28: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Constants

"#+-#&KX0)"(/-4-YZU[\]Z^_Y]\-:-YZU[]`\---------------:-D5-?-YZU[abcd\F-:-YZU[_e]b`6

!'(,"#(-YZU[\]Z^_Y]\->B!'(,"#(-YZU[]`\-8!'(,"#(-YZU[abcd\-B!'(,"#(-YZU[_e]b`-5

"#+-#&KX0)"(/-4->B-:-8--------------:-D5-?-BF-:-56

"#+-#&KX0)"(/-4->J6

28

Magic Number

Own line (at top)No =No ;}

Page 29: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Constants

29

!'(,"#(-YZU[\]Z^_Y]\->B

YZU[\]Z^_Y]\-4-B6

>B-4-B6

"#+-YZU[]`\-4-86

YZU[]`\-4-B6

111

111

Error

Page 30: Lecture 4 - dgp.toronto.edujdeber/.../APS105-Lecture-4.pdf · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 4 January 23, 2012

Constants and Style

30

!'(,"#(-dcZb-8!'(,"#(-]fc[acLY][cY_-51B

!'(,"#(-]SLb]g-GB

Bad Style

Evil

"#+-#&KX0)"(/-4->J6 Magic NumberBad Style