week 1 - cdn.cs50.netcdn.cs50.net/2007/fall/lectures/1/week1.pdf · week 1. 1 scratch versus c. 2...
TRANSCRIPT
![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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/1.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/2.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/3.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/4.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/5.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/6.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/7.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/8.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/9.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/10.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/11.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/12.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/13.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/14.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/15.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/16.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/17.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/18.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/19.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/20.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/21.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/22.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/23.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/24.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/25.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/26.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/27.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/28.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/29.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/30.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/31.jpg)
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,](https://reader036.vdocuments.mx/reader036/viewer/2022070712/5edfa37fad6a402d666af801/html5/thumbnails/32.jpg)
31
Computer Science 50Introduction to Computer Science I
Harvard College
David J. [email protected]
Week 1