Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2...

32
0 Computer Science 50 Introduction to Computer Science I Harvard College David J. Malan [email protected] Week 1

Transcript of Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2...

Page 1: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

0

Computer Science 50Introduction to Computer Science I

Harvard College

David J. [email protected]

Week 1

Page 2: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

1

Scratch versus C

Page 3: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

2

Hello, C!#include <stdio.h>

int

main(int argc, char * argv[])

{

printf("hello, world\n");

}10000011 00000001 00010001 00000000 00111101 11111100 01110100 0011110100000000 01000000 00000000 00000000 00000000 00000000 00000000 0000000010010000 00000000 00000000 00000000 01010000 00000000 00000111 0011000000001011 00000001 00001011 00000011 00001010 00000000 00000000 0000000000000000 00100000 00000000 00000000 00000000 00000000 00000000 0000000000000000 00100000 00000000 00000000 00000000 00000000 00000000 0000000000000000 00000000 00000000 00000000 00000000 00000000 00000000 0000000001110000 00010000 00000000 00100000 00000001 00000000 00000000 0000000000000000 00000000 00000000 00100000 00000001 00000000 00000000 0000000000000000 00000000 00000000 01000000 00000001 00000000 00000000 0000000000000000 00100000 00000000 01000000 00000001 00000000 00000000 0000000011111111 11111111 11111111 11111111 11111111 11111111 11111111 1111111110010000 10000000 00000000 01000000 00000001 00000000 00000000 0000000000101110 01100100 01111001 01101110 01100001 01101101 01101001 0110001110110000 00000100 00000000 00100000 00000001 00000000 00000000 0000000010110000 00000100 00000000 00100000 00000001 00000000 00000000 0000000010100000 00000001 00000000 00000000 00000000 00000000 00000000 0000000010110000 00000100 00000000 00000000 00000000 00000000 00000000 0000000000000000 00000000 00000000 00000000 00000000 00000000 00000000 0000000000000000 00000000 00000000 00000000 00000000 00000000 00000000 0000000000000000 00000000 00000000 00000000 00000000 00100000 00000000 00000000[...]

Page 4: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

3

nice.fas.harvard.edu

Page 5: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

4

nice.fas.harvard.edu

Page 6: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

5

nice.fas.harvard.edu

Page 7: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

6

Some Commandscd

cp

ls

mkdir

mv

pwd

rm

Page 8: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

7

Some More Commandsci

co

cs50submit

emacs

gcc

gdb

make

man

nano

sb

vi

Page 9: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

8

How to Write a Program in C

1) nano hello1.c

2) gcc hello1.c

3) a.out

seehello1.c

Page 10: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

9

How to Write a Program in C(with a better name)

1) nano hello1.c

2) gcc -o hello1 hello1.c

3) hello1

seehello1.c

Page 11: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

10

main

int main(int argc, char * argv[]);

Page 12: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

11

Standard Outputprintf

int printf(const char *format, ...);

seehttp://www.cppreference.com/stdio/printf.html

Page 13: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

12

Escape Sequences\n

\r

\t

\"

\\

seehttp://www.cppreference.com/escape_sequences.html

Page 14: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

13

VariablesTypes †

char

double

float

int

† long, short, signed, unsigned

seemath1.c

Page 15: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

14

Format Strings%c

%d

%e

%E

%f

%s

%u

%x

seemath2.c, sizeof.c

http://www.cppreference.com/stdio/printf.html

Page 16: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

15

Arithmetic Operators+

-

*

/

%

Page 17: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

16

Precedence

Chart from http://www.difranco.net/cop2220/op-prec.htm.

Page 18: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

17

Width and Precision †

%<width>.<precision>e

%<width>.<precision>E

%<width>.<precision>f

%<width>.<precision>s

† -, +

seemath3.c

http://www.cppreference.com/stdio/printf.html

Page 19: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

18

VariablesTypes †

bool

string

† These are CS 50-specific.

seehello2.c

Page 20: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

19

Standard Input †

char GetChar();

double GetDouble();

float GetFloat();

int GetInt();

string GetString();

† These are CS 50-specific.

seehello3.c, adder.c

Page 21: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

20

How to Write a Program in C(using CS 50’s library)

1) nano hello3.c

2) gcc -o hello3 hello3.c -lcs50

3) hello3

seehello3.c

Page 22: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

21

Fahrenheit to CelsiusC = (5/9) × (F – 32)

#include <cs50.h>

#include <stdio.h>

int

main(int argc, char * argv[])

{

}

Page 23: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

22

Conditionsif

if (condition){

/* do this */}

Page 24: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

23

Conditionsif-else

if (condition){

/* do this */}else{

/* do that */}

seeconditions1.c

Page 25: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

24

Conditionsif-else if-else

if (condition){

/* do this */}else if (condition){

/* do that */}else{

/* do this other thing */}

seeconditions2.c

Page 26: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

25

Boolean Expressionsif (condition || condition){

/* do this */}else{

/* do that */}

Page 27: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

26

Boolean Expressionsif (condition && condition){

/* do this */}else{

/* do that */}

seenonswitch.c

Page 28: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

27

Conditionsswitch

switch (expression){

case i:/* do this */break;

case j: /* do that */break;

default:/* do this other thing */

}

seeswitch{1,2}.c

Page 29: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

28

Loopsfor

for (initializations; condition; updates){

/* do this again and again */}

seeprogress{1,2}.c

Page 30: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

29

Loopswhile

while (condition){

/* do this again and again */}

seeprogress3.c

Page 31: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

30

Loopsdo-while

do{

/* do this again and again */}while (condition)

seepositive.c

Page 32: Week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · Week 1. 1 Scratch versus C. 2 Hello, C! #include  int main(int argc, char * argv[]) {printf("hello,

31

Computer Science 50Introduction to Computer Science I

Harvard College

David J. [email protected]

Week 1