0% found this document useful (0 votes)
55 views220 pages

Lecture 1

Uploaded by

hsn.balorss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views220 pages

Lecture 1

Uploaded by

hsn.balorss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 220

learn how to program in Scratch

learn how to program in C


learn how to solve problems
learn how to solve problems with functions
learn how to solve problems with variables
learn how to solve problems with conditionals
learn how to solve problems with loops
learn how (not) to solve problems
#include <stdio.h>

int main(void)
{
printf("hello, world\n");
}
#include <stdio.h>

int main(void)
{
printf("hello, world\n");
}
source code
01111111 01000101 01001100 01000110 00000010 00000001 00000001 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000010 00000000 00111110 00000000 00000001 00000000 00000000 00000000
10110000 00000101 01000000 00000000 00000000 00000000 00000000 00000000
01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
11010000 00010011 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 01000000 00000000 00111000 00000000
00001001 00000000 01000000 00000000 00100100 00000000 00100001 00000000
00000110 00000000 00000000 00000000 00000101 00000000 00000000 00000000
01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
01000000 00000000 01000000 00000000 00000000 00000000 00000000 00000000
01000000 00000000 01000000 00000000 00000000 00000000 00000000 00000000
11111000 00000001 00000000 00000000 00000000 00000000 00000000 00000000
11111000 00000001 00000000 00000000 00000000 00000000 00000000 00000000
00001000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000011 00000000 00000000 00000000 00000100 00000000 00000000 00000000
00111000 00000010 00000000 00000000 00000000 00000000 00000000 00000000
...
machine code
input → → output
source code →
source code → → machine code
source code → compiler → machine code
Visual Studio Code for CS50
cs50.dev
#include <stdio.h>

int main(void)
{
printf("hello, world\n");
}
code hello.c

make hello

./hello
arguments → function → side effects
→ →
printf("hello, world\n");
printf("hello, world\n");
printf("hello, world\n");
printf("hello, world\n");
printf("hello, world\n");
printf("hello, world\n");
printf("hello, world\n");
escape sequences
header files
libraries
Manual Pages
manual.cs50.io
stdio.h
manual.cs50.io/#stdio.h
manual.cs50.io/3/printf
cs50.h
manual.cs50.io/#cs50.h
get_char

get_double

get_float

get_int

get_long

get_string

...
get_char

get_double

get_float

get_int

get_long

get_string

...
arguments → function → return value
→ →
string answer = get_string( );
printf("hello, %s", answer);
string answer = get_string("What's your name? ");
printf("hello, %s", answer);
string answer = get_string("What's your name? ");
printf("hello, %s", answer);
string answer = get_string("What's your name? ");
printf("hello, %s", answer);
string answer = get_string("What's your name? ");
printf("hello, %s", answer);
string answer = get_string("What's your name? ");
printf("hello, %s", answer);
printf("hello, %s", answer);
printf("hello, %s\n", answer);
printf("hello, %s\n", answer);
printf("hello, %s\n", answer);
printf("hello, %s\n", answer);
printf("hello, %s\n", answer);
types
bool

char

double

float

int

long

string

...
bool

char

double

float

int

long

string

...
bool

char

double

float

int

long

string

...
get_char

get_double

get_float

get_int

get_long

get_string

...
get_char

get_double

get_float

get_int

get_long

get_string

...
format codes
%c

%f

%i

%li

%s
%c

%f

%i

%li

%s
%c

%f

%i

%li

%s
conditionals
if (x < y)
{
printf("x is less than y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else
{
printf("x is not less than y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else
{
printf("x is not less than y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else
{
printf("x is not less than y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else if (x == y)
{
printf("x is equal to y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else if (x == y)
{
printf("x is equal to y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else if (x == y)
{
printf("x is equal to y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else
{
printf("x is equal to y\n");
}
variables
int counter = 0;
int counter = 0;
int counter = 0;
int counter = 0;
counter = counter + 1;
counter = counter + 1;;
counter += 1;
counter++;
counter--;
bool

char

double

float

int

long

string

...
bool

char

double

float

int

long

string

...
get_char

get_double

get_float

get_int

get_long

get_string

...
get_char

get_double

get_float

get_int

get_long

get_string

...
loops
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int counter = 3;
while (counter > 0)
{
printf("meow\n");
counter = counter - 1;
}
int i = 3;
while (i > 0)
{
printf("meow\n");
i = i - 1;
}
int i = 3;
while (i > 0)
{
printf("meow\n");
i -= 1;
}
int i = 3;
while (i > 0)
{
printf("meow\n");
i--;
}
int i = 3;
while (i > 0)
{
printf("meow\n");
i--;
}
int i = 1;
while (i <= 3)
{
printf("meow\n");
i++;
}
int i = 0;
while (i < 3)
{
printf("meow\n");
i++;
}
int i = 0;
while (i < 3)
{
printf("meow\n");
i++;
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
while (true)
{
printf("meow\n");
}
while ( )
{

}
while (true)
{

}
while (true)
{
printf("meow\n");
}
void meow(void)
{
printf("meow\n");
}
int main(void)
{
for (int i = 0; i < 3; i++)
{
meow();
}
}
void meow(int n)
{
for (int i = 0; i < n; i++)
{
printf("meow\n");
}
}
int main(void)
{
meow(3);
}
arguments → function → side effects
arguments → function → return value
+

%
scope
Linux
graphical user interface
GUI
command-line interface
CLI
cd

cp

ls

mkdir

mv

rm

rmdir

...
constants
comments
0000
0001
0010
0011
0100
0101
0110
0111
1000
integer overflow
00000000000000000000000000000000
11111111111111111111111111111111
4294967295
2147483647
-2147483648
bool

char

double

float

int

long

string

...
bool

char

double

float

int

long

string

...
get_char

get_double

get_float

get_int

get_long

get_string

...
get_char

get_double

get_float

get_int

get_long

get_string

...
%c

%f

%i

%li

%s
%c

%f

%i

%li

%s
truncation
bool

char

double

float

int

long

string

...
bool

char

double

float

int

long

string

...
get_char

get_double

get_float

get_int

get_long

get_string

...
%c

%f

%i

%li

%s
type casting
floating-point imprecision
1999
1999
1900
19 January 2038
13 December 1901
10 × (level + 4)
correctness, design, style

You might also like