Week 1
Scratch vs. C
-
Now that we’ve explored some basic programming concepts with Scratch, we can try to use the same ideas with a more traditional language, C.
-
Recall that last week, to run our program in Scratch, we would begin with a block that read
when green flag clicked
. -
Our example of having Scratch say
hello, world
can be translated to the following C:#include <stdio.h> int main(void) { printf("hello, world\n"); }
-
printf
is the equivalent ofsay
in Scratch, and it will print whatever is inside the parentheses. -
We notice a bit of syntax, like the double quotes and the semicolon, but we can focus on one piece at a time.
-
-
In Scratch,
say
was a function that took an argument, or parameter, and the equivalent line in C is:printf("hello, world\n");
-
The
\n
prints a new line, like pressing enter after typing out that message.
-
-
And in the case of loops, in Scratch we might have a
forever
block that does something over and over again. In C, we would have this:while (true) { printf("hello, world\n"); }
-
The statements inside the braces will be executed again and again
while
the expression inside the parentheses is true, and sincetrue
will always be true, the loop will continue forever.
-
-
To repeat a loop a certain number of times, we have something a little more complex:
for (int i = 0; i < 50; i++) { printf("hello, world\n"); }
-
We’ll come back to this again in a bit, but know that
for
is the special word we use to start a loop.
-
-
In Scratch, we used blocks for variables like
set [i] to [0]
to store values. To do the same in C, we’d do this:int i = 0;
-
int
stands for integer, where we are creating a variable to store whole numbers,i
is the name of our new variable, and0
is the value we will initially set it to. -
And the semicolon just ends this statement.
-
-
Boolean expressions were questions that would either be true or false, and in Scratch they might have looked like
i < 50
, isi
less than50
. And in C, it’s just as simple:i < 50
-
x < y
, too, is the same in Scratch and C, as long asx
andy
are both variables we’ve created and assigned values to. -
We can use conditions, too, to create forks in the road. Recall that last time we demonstrated this in Scratch:
-
The same might look even a little simpler in C:
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"); }
-
Scratch had lists, too, where we could store multiple values together. The equivalent in C is something called an array, where we store lots of items back-to-back.
-
Last time in Scratch we saw blocks like
item (1) of [argv]
, which took the first item from a list calledargv
, and in C (we start counting from 0 in C, since that’s the smallest non-negative value we can represent), we would useargv[0]
.
hello, C
-
So, going back to our original example:
#include <stdio.h> int main(void) { printf("hello, world\n"); }
-
main
is the equivalent ofwhen green flag clicked
, and marks the main chunk of code that should be executed.
-
-
To go from this code, which is readable to humans, need to be translated first to machine code, that look something like this:
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 00111000 00000010 01000000 00000000 00000000 00000000 00000000 00000000 00111000 00000010 01000000 00000000 00000000 00000000 00000000 00000000 00011100 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ...
-
You’ll be asked to write this from memory for the test, so start memorizing now! Just kidding.
-
-
But you do need to remember that, at the end of the day, computers only operate with binary,
0
s and1
s, and so each of these patterns of0
s and1
s represent a special instruction to the CPU, central processing unit, of the computer. Some patterns will mean "print this to the screen," some patterns "add these two numbers," or any of a large number of operations. -
We don’t need to create this by hand, since there is software called compilers, which take code written in C and readable by humans (source code), and translates it to machine code.
-
We’re all using slightly different operating systems on our computer, like macOS or Windows or others, and just so everyone is on the same page (get it?), we’ll use a cloud-based integrated development environment called CS50 IDE.
-
What does that actually mean? This is a web-based programming environment based on a platform called Cloud9, which allowed us to pre-install standard software and configure it the same way for everyone.
-
-
We can visit the page (heh), create a free account, and see something like this:
-
On the left is where we can see our files in the cloud, on the right is where we edit our code, and the strange box at the bottom is called a
terminal
, a command-line interface (CLI) where we can type in commands directly to our computer. In this case, these commands will be sent to the computer in the cloud, and we’ll use it to compile our code or run our programs.
-
-
We’ll jump right in with making our first program, and first we’ll save a file using
File > Save as
: