01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003...

70
APS105 APS105 Introduction 1

Transcript of 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003...

Page 1: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

APS105APS105

Introduction

1

Page 2: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Welcome

• To University and UofToronto

• To Engineering• To Engineering

T APS10• To APS105

2

Page 3: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

About your Prof• Prof. Greg Steffan

– PhD Computer Science, Carnegie Mellon, 2003– BASc/MASc, ECE, UofToronto, 1995/1997

• Research– Making multicores and FPGAs easier to program

• Contact:Contact:– [email protected]– Office: EA321Office: EA321– Office hours: wed 12-1pm

3

Page 4: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Why Learn About Programming and C?

4

Page 5: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Why Learn Programming?• Computers: our most powerful technology

– Learn to command them to do your bidding!• For love of design

– Programming is pure design with few limitationsg g p g– “only slightly removed from pure thought-stuff”

• Entrepreneurial opportunitiesEntrepreneurial opportunities– Start a company in your bedroom with no $$$

• Many engineering jobs involve programming• Many engineering jobs involve programming– Simulations, databases, websites

Wh th i il l t i l h i l– Whether civil, electrical, mechanical…

5

Page 6: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Why Learn C?• Lots of other newer, sexier languages

– Java, Python, Ruby, javascript• Benefits of C:

– “Compiled language”, not “interpreted”p g g , p• Close to the hardware with no layers between• Easier to understand what is happening• Important building-block for later computing courses

– C/C++ still very common• In high-performance applications• In low-level code like operating systems, databases,…

C lik t f d i f b l– C-like-syntax found in many of above languages

6

Page 7: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Why Attend Lectures?T l thi k th h bl• To see someone else think through problems– Different than simply seeing the solution

L “ h ” t j t “h ”• Learn “why”, not just “how”– how course material fits in with “big picture”

• Will provide proper emphasis– Highlight key material– Overview-only sundry material

• Will teach material in order of importance– Eg., order that you need it for labs

• Ideally more entertaining than reading manual– Else Prof-fail.

7

Page 8: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Administration

8

Page 9: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Course Website• Portal.utoronto.ca

– APS105FALL 2011• Any Handouts

– Lab Assignmentsg– Past Exams– Form for missed labs/quizzesForm for missed labs/quizzes– Form for misgraded midterm

• Discussion Board• Discussion Board

9

Page 10: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Course Meetings• Lecture Periods:

– three one-hour lectures per week– Start at 10mins after the hour

O ti l L t• Optional Lecture:– review of regular lecture material– attendance recommended for non-expertsattendance recommended for non experts– tuesdays 3-4pm MC102 (entire course invited)

• Tutorial Periods:– One per week, run by TAs– Quizes in about half of the tutorials

Attendance optional unless there is a quiz– Attendance optional, unless there is a quiz• Lab Periods:

– Two hours per weekTwo hours per week– TAs available to answer questions– Attendance "mandatory" 10

Page 11: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Evaluation• Quizzes: 8%

– 4 total– Given in tutorials– Based on lecture material since last quiz

• Labs: 17%– 8 total8 total

• Midterm: 25%• Final Exam: 50%• Final Exam: 50%

– Emphasis on 2nd half of course

11

Page 12: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Lecture Slides and Note-Taking• Electronic slides with fill-in-the-blanks

– A hybrid between powerpoint and chalkboard• Pptx/pdf available on www prior to lecture

– Please print and bring to classp g• The filled-in “blanks” are only during class

– Filled-in slides will not be published– Filled-in slides will not be published• The symbol that indicates a blank to fill

12

Page 13: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Course Textbook• “An Introduction to Computer Science

Using C”, 2nd Edition– by John Carter– Published by McGraw Hill-Ryerson

• How to get a copy:– UofT bookstore– basement of Sandford Fleming Building– Amazon etc.Amazon etc.

13

Page 14: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Labs (programming assignments)• There are 8 of them in total

– You will always be working on one• Must run properly on ECF systems

– Even if it runs fine elsewhere• Marks for functionality and style

– Proper style will be defined throughout term– Proper style will be defined throughout term

14

Page 15: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Cheating/Copying• Easy to do, equally easy to catch!

– “cheater-beater” software• Automatically compares all submissions

– we do actually do this, and do follow through!• the interview and punishment process is painful for all!

• You only cheat yourself– Labs worth 1.6% each– The practice you get by doing them is invaluable– The consequences for being caught are harsh

• See Engineering Calendar for details

15

Page 16: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Getting Help• See course info handout• Meet some friends: “study groups”

– Collaborating vs cheating• Ask questions in classAsk questions in class

– Point out confusing things and mistakes!• Get help quickly if you get into trouble• Get help quickly if you get into trouble

– First-year office (1st floor Galbraith Building)l t t– www.calss.utoronto.ca

• Help with academics, personal

16

Page 17: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

In-Class Etiquette• Be on time

– Class starts at 10mins after the hour• Quiet please!

– Silence yourself and all electronic devices!y• Pay attention

– Eg watching youtube on laptop distracts others– Eg., watching youtube on laptop distracts others• Ask questions and point out errors!

R i h d– Raise your hand– Say “question!” if I don’t notice

17

Page 18: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

How to Succeed at APS105• Attend lectures

– take notes, ask questions• Do labs yourself, on-time

– Practice/experience is invaluablep• Review lecture material regularly

– Be better prepared for quizes/midterm/final– Be better prepared for quizes/midterm/final– Avoid last-minute cramming

• Prepare for midterm/final• Prepare for midterm/final– Practice by trying past midterms/finals

P ti b filli l t lid– Practice by filling lecture slides on your own• Both available on www

18

Page 19: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

How to Succeed at Skule (computing)• Learn to keyboard properly!• Learn unix/linux basics

– see the unix handout• has details on how to log in from home

• Learn some javascript– For programming www page featuresFor programming www page features

• Write a simple app for your own useEg for your laptop or iphone– Eg., for your laptop or iphone

19

Page 20: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Computer Systems Basics

20

Page 21: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Computer Systems Stack

ApplicationsJ ( d th )Java (and others)Java interpreter (JVM, and others)C (and others) APS105

C CompilerProcessorsLogic gatesTransistorsElectrons and materials

21

Page 22: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

How We Tell a Processor What to Do

Programmer Machine InstructionsCompiler Processor

0100101Add r3,r3,r1Cmp r3,r1Bge 0x3340a

l 3 2

intFoo (int x){return x + 5;

}

0100101

Mulu r3,r5,r2Sub r1,r3,r4…

}…

Processors execute machineProcessors execute machine instructions encoded in binary

22

Page 23: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

A TYPICAL PC

Graphics card

Motherboard (CPU, MEMORY)Hard drive

C / /

Monitor

CD/DVD R/W

USB ConnectorsKeyboardMouse

Power Supply

23

Page 24: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Simple View of a Motherboard

Memory (RAM)

CPU

• Memory: – holds bits– can be read from or written to

• CPU: datapath arithmetic/logic nits (add s b) etc– datapath: arithmetic/logic units (add, sub), etc.

– control circuitry

24

Page 25: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

C

25

Page 26: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

About C• Invented in Bell Labs (in 1970s)

– by Dennis Ritchie– Inherits from earlier language

• called “B”

• C++ – an extension of C

Dennis Ritchie

– Supports “object oriented programming”

26

Page 27: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Hello World

.

27

Page 28: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Alternative Hello World

.

28

Page 29: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

GCC• gcc• gcc

– Means “GNU C compiler”– A program that converts C to 0’s & 1’sA program that converts C to 0 s & 1 s

• Stored in a file called an “executable”

• Type this command to compile hello.cType this command to compile hello.c

• Generates the executable file• Generates the executable file

Insteady you could type:• Insteady you could type:

• Generates the executable file29

Page 30: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Value Representation

30

Page 31: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Bits and Bytes• “bit” is short for “binary digit”

– A bit can be 0 or 1• Why do computers use bits?

A b t i f 8 bit• A byte is a group of 8 bits– 1 byte can be written as 1B

31

Page 32: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

RECALL: BASE 10 (decimal) • 956• 956

• NOTE: not all languages/cultures use base10– Ndom Frederik Hendrik Island: base6Ndom, Frederik Hendrik Island: base6– nimbia: base12– Huli, Papua New Guinea: base15

T t il M i b 20– Tzotzil, Mexico: base20– Computers use base2 (binary)

32

Page 33: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

USING BINARY (BASE 2)• denoted by ‘0b’ (in this course, in C-code)• Converting from binary to decimal:

• 0b1011• 0b1011

33

Page 34: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Questions• How many decimal numbers can you

represent with N bits?

• NOTE: – computers can only represent finite numbers!

34

Page 35: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Byte Size• 1byte = 1B = 8bits

35

Page 36: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

What is this?10010010

36

Page 37: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Types

37

Page 38: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Types• Data Type:

– tells computer how to interpret a group of bits– Without a type they are just bits!

• C has types:yp– Basic data types: built-in, already defined for you– User-defined types: name your own! (later)User defined types: name your own! (later)

• Basic data types:A single value of a certain size (bytes)– A single value of a certain size (bytes)

– Example: a number or a character

38

Page 39: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Integer Types32bit l 231 231 1• int: 32bits, values: -231 .. 231-1

– We will almost always use int in this class• Other integer types:

– unsigned int: 32bits, values 0.. 232-1g– short: at least 16bits– long: at least 32bitsg– long long: at least 64bits– what does "at least" mean?what does at least mean?

• the actual size can depend on the system!!!!!

39

Page 40: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Floating Point Types• How to use binary to represent real numbers?

– Eg., 1.3333333333333….– Can’t: approximate, with limited accuracy

• Floating-point: a common approximationg p pp– A value (mantissa) and an exponent– Flexible: can represent very big or very smallFlexible: can represent very big or very small

• Eg., 2.1313*1020 or 2.1313*10-20

– We don’t need to know the details for this class

40

Page 41: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Floating Point Types in Ct l t 64 bit• double: at least 64 bits

• We will almost always use double in this class

• float: at least 32 bits

41

Page 42: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Character Type• representing keyboard characters:

– Eg: a,b,c,d…A,B,C,D, !@#$%– Standard encoding called ASCII:

• eg., ‘A’ = 65 = 0100 0001 (binary)

• char: 8 bits, typically ASCII-encoded• Note1: char holds only a single charactero e c a o ds o y a s g e c a ac e

– How can we represent a “string” of characters?• Note2: recall “escapes”• Note2: recall escapes

– Eg., ‘\n’ represents the return character• Encoded as a single ASCII value• Encoded as a single ASCII value

42

Page 43: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Boolean Type• Often you want to represent true/false

– The old way: use int values 0 and 1– The new way: use the bool type

• bool typeyp– Has values ‘true’ and ‘false’– Note: you have to include the bool libraryNote: you have to include the bool library

• in program (at top): #include <stdbool.h>

43

Page 44: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Identifiers and Variables

44

Page 45: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Identifiers• Identifiers give names to things

– Choosing good names is a key skill!• Rules: identifiers can be

– any length (any number of characters)y g ( y )– upper & lower case letters, digits, ‘_’– First character cannot be a digitFirst character cannot be a digit– Cannot match a C “reserved” word (like “int”)

45

Page 46: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Choosing Identifiers: Good Style• Use upper-case to separate words:

• Or use underscores to separate words:

• Avoid easily-confused characters:

• Choosing good identifiers is an art:

46

Page 47: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Declaring VariablesV i bl t f l f t i t• Variable: storage of a value of a certain type

• Variable declaration:– specifies that you plan to use a variable– and you would like it to be a certain type– Note: the C-compiler creates storage for it

• General form:<type> <identifier list>;

47

Page 48: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Declaring Variables: Examples• Example declarations:

48

Page 49: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Assigning Values• Can assign a value at declaration, or later• Example assignment at declaration:

• Example assignment at a later point:

• Accuracy:

49

Page 50: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Output

50

Page 51: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Printing to the Screen on PC

Graphics card

Motherboard (CPU, MEMORY)Hard drive

CD/DVD R/W

Monitor

CD/DVD R/W

USB ConnectorsUSB ConnectorsKeyboardMouse Power Supply

.

51

Page 52: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Printing Values• the printf function prints to the screen

printf(“hi mom\n”);

• printf can also print variable valuesprintf can also print variable valuesint age = 39;printf(“I am %d years old\n” age);printf( I am %d years old\n ,age);.

52

Page 53: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Printing Integers• decimal (base10)

– %d: print the decimal using space as necessary– %4d: print the decimal using at least 4 spaces

• Example:p.

53

Page 54: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Printing chars• char

– %c: print a single character• Example:

.

54

Page 55: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Printing doubles• %lf

– means “long float” (d is already used!)– by default prints 6 digits right of the decimal– cheat: can use %f instead, does the same thing

• %.2lf– print 2 digits right of the decimalprint 2 digits right of the decimal

• %6.2lfminimum of 6 digits 2 right of decimal– minimum of 6 digits, 2 right of decimal

• %et f– use exponent form: ±x.xxxxxxe±nn

55

Page 56: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Printing doubles: Examples• Examples:

.

56

Page 57: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Printing bool• No conversion specification for bool

– Just use %d (decimal)– Prints 0 for false and 1 for true

• Example:p.

57

Page 58: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Special Cases• How do you print ‘%’?

– The ‘%’ is the ‘escape’ for conversion specs!– Answer: use ‘%%’

• Example:p.

• How do you print ‘\’?• How do you print \ ?– The ‘\’ is also an ‘escape’ character

Answer: use ‘\\’– Answer: use \\

58

Page 59: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Input

59

Page 60: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Input from the KeyboardGraphics card

Motherboard (CPU MEMORY)Hard drive

Graphics card

Monitor(CPU, MEMORY)

CD/DVD R/W

USB ConnectorsKeyboardMouse Power Supply

.

60

Page 61: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Input from the Keyboard: scanf• the scanf function inputs from the keyboard

61

Page 62: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Inputting Multiple Values• scanf automatically skips whitespace

– eg., spaces, tabs, newlines• .

62

Page 63: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Consuming Whitespace• If input string is:

– “ 22 33 44”• And I execute:

– scanf(“%d%d”,&x,&y); ( , , y);• The next scanf call would start at this point:

– “ 22 33 44”– 22 33 44

63

Page 64: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Skipping Characters• Can manually skip specific characters

– eg., non-whitespace– include them in the control string

• Example:p.

64

Page 65: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Inputting Chars• Example:

int year;char term;scanf(“%d%c”,&year,&term);

• Note: %c does not skip whitespace!• .

65

Page 66: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Constants

66

Page 67: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Constants• A constant is a value that does not change

– possibly used in multiple places in your program• Literal constant:

– the value is written directly in your codey y– example: int age = 2;

• named constant:named constant:– assign a constant value an identifier

then use the identifier in your code– then use the identifier in your code– Named constants make for easier-to-read code!

67

Page 68: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Named Constants Example• With literal constant:

.

• With named constant:.

68

Page 69: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Two Methods for Named Constants• Using the const attribute

– General form: const <type> <identifier> = <value>;

const double PI = 3.14159;

– const attribute promises value will not change• Using the #define compiler directiveg p

#define PI 3.14159

– note1: directives go at top of fileg p– note2: directives have no ‘=‘, no ‘;’– compiler does a search-and-replace of PIcompiler does a search and replace of PI

• with the value 3.14159 everywhere PI appears69

Page 70: 01 intro · About your Prof • Prof. Greg Steffan – PhD Computer Science, Carnegie Mellon, 2003 – BASc/MASc, ECE, UofToronto, 1995/1997 • Research – Making multicores and

Named Constants for Good Style• avoid magic numbers in your code

– numbers which have no meaning to others– use of magic numbers will cost you points in labs!

• avoid multiple copies of the same valuep p– if you must update the value, only have to do it in

one place in the code• Convention/Good-Style:

– use capital letters for named constant identifiersuse capital letters for named constant identifiers– eg., PI and MAX_ATTEMPTS

70