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

Post on 03-Jun-2020

4 views 0 download

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

0

Computer Science 50Introduction to Computer Science I

Harvard College

David J. Malanmalan@post.harvard.edu

Week 1

1

Scratch versus C

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[...]

3

nice.fas.harvard.edu

4

nice.fas.harvard.edu

5

nice.fas.harvard.edu

6

Some Commandscd

cp

ls

mkdir

mv

pwd

rm

7

Some More Commandsci

co

cs50submit

emacs

gcc

gdb

make

man

nano

sb

vi

8

How to Write a Program in C

1) nano hello1.c

2) gcc hello1.c

3) a.out

seehello1.c

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

10

main

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

11

Standard Outputprintf

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

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

12

Escape Sequences\n

\r

\t

\"

\\

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

13

VariablesTypes †

char

double

float

int

† long, short, signed, unsigned

seemath1.c

14

Format Strings%c

%d

%e

%E

%f

%s

%u

%x

seemath2.c, sizeof.c

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

15

Arithmetic Operators+

-

*

/

%

16

Precedence

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

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

18

VariablesTypes †

bool

string

† These are CS 50-specific.

seehello2.c

19

Standard Input †

char GetChar();

double GetDouble();

float GetFloat();

int GetInt();

string GetString();

† These are CS 50-specific.

seehello3.c, adder.c

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

21

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

#include <cs50.h>

#include <stdio.h>

int

main(int argc, char * argv[])

{

}

22

Conditionsif

if (condition){

/* do this */}

23

Conditionsif-else

if (condition){

/* do this */}else{

/* do that */}

seeconditions1.c

24

Conditionsif-else if-else

if (condition){

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

/* do that */}else{

/* do this other thing */}

seeconditions2.c

25

Boolean Expressionsif (condition || condition){

/* do this */}else{

/* do that */}

26

Boolean Expressionsif (condition && condition){

/* do this */}else{

/* do that */}

seenonswitch.c

27

Conditionsswitch

switch (expression){

case i:/* do this */break;

case j: /* do that */break;

default:/* do this other thing */

}

seeswitch{1,2}.c

28

Loopsfor

for (initializations; condition; updates){

/* do this again and again */}

seeprogress{1,2}.c

29

Loopswhile

while (condition){

/* do this again and again */}

seeprogress3.c

30

Loopsdo-while

do{

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

seepositive.c

31

Computer Science 50Introduction to Computer Science I

Harvard College

David J. Malanmalan@post.harvard.edu

Week 1