The Course Environment
A programmer uses two main tools: a text editor and a terminal. Outside this course you need to download and install them to your computer, but here they are included in the pages of the course itself.
The Text Editor
What is a program? It's a series of instructions, written in a particular programming language, that the computer will carry out when we run the program.
We write our programs in a text editor. Just like when we write a report or a letter we use Microsoft Word, when we write a program we use a text editor such as Emacs or Sublime, that has been designed for writing code.
Type this code into the editor window below:
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
Notice how the editor helps you by automatically indenting certain lines, and auto-completing braces. It also colours different parts of the program to make it easier to read. Realise though that the program is just text, just like a Microsoft Word document. Nothing magical or mysterious is happening; it's just a text file.
Don't worry about understanding the code for now; we'll come to that soon, although perhaps you can already guess what it does.
In each lesson you may find multiple editor windows. Each one represents a different file in the course environment.
The Terminal
You may be old enough to remember the Commodore 64 or Amiga computers from the 1980s. The way you ran software in those days wasn't by double-clicking on icons like we do now, but by typing commands into something called a terminal, which looked something like this:
Terminals are still used by programmers today, and you probably have one installed on your computer. On Windows computers it's called Command Prompt, and on Macs it's called Terminal, but you don't need either of those for this course. Here is the terminal we'll be using:
In each lesson, you may see multiple terminals. That's just for convenience, so you don't have to keep scrolling up and down the page. Each terminal is identical, and each one connects to the same environment. You can think of the terminal as accessing your own virtual computer that is just used for this course.
Some common terminal commands
ls
Type ls
into the terminal and press enter.
ls
is short for list, and it lists all the files in the current directory (directory just means folder).
You should see a single file named hello.c
. That's the file containing the program we just wrote. The name of the file is hello
, and .c
is called the file extension. You've probably seen file extensions before, like .doc
for Microsoft Word documents, or .jpg
for images. Here .c
means that the file is a program written in the programming language called C. File extensions tell the computer what type of information the file contains.
make
The file hello.c
is just text, and believe it or not the language C has been designed for humans to read, not computers. We can't just run it directly - the computer doesn't understand it - we first have to translate it, or compile it, into a format that the computer can understand and execute.
Enter the command make hello
.
You shouldn't see any message in the terminal. If you get an error message, make sure you've typed the program correctly. Now enter ls
again. You should now see two files: hello.c
and hello*
. This new file is also called hello
, but it isn't a .c
file. The asterisk means that this is an executable file.
What happened when we used the command make hello
? Three things happened. First, the computer took your source code, hello.c
and compiled it into something called assembly language, which looks something like this:
main:
push RBP
mov RBP, RSP
sub RSP, 16
lea RDI, QWORD PTR [.L.str]
mov DWORD PTR [RBP - 4], 0
mov AL, 0
call printf
mov ECX, DWORD PTR [RBP - 4]
mov DWORD PTR [RBP - 8], EAX
mov EAX, ECX
add RSP, 16
pop RBP
ret
.L.str:
.asciz "hello, world\n"
Second, the computer took this assembly code and assembled it into binary, which is just zeroes and ones.
Thirdly, the computer does something called linking, which links our programs to any libraries we've included in our program (stdio.h
is a "library", which we'll learn about later), to produce a single executable file that the computer can execute.
Programmers commonly refer to these three steps - compiling, assembling and linking - as just compiling. You don't need to worry about the individual steps, you can just run the command
make
and think of this simply as compiling your code, from text into an executable format.
./
To execute our executable file, i.e. to run our program, enter the command ./hello
in the terminal.
rm
To delete files, use the command rm
followed by the name of the file, for example rm hello
(we don't include the asterisk when referring to the executable file). To create the file again, just go back to the editor window and start typing, this will create and auto-save the file.
Exercises
1. Delete the files hello.c
and hello
If you haven't already deleted the files, delete them both now. Verify that they've been deleted by listing the files - the directory should be empty.
2. Compile and run the following program:
#include <stdio.h>
int main(void)
{
int a = 2;
int b = 4;
int c = a + b;
printf("c is %i\n", c);
}
Note that the file is called exercise.c
and not hello.c
.
If you've done it correctly you will see the output c is 6
in the terminal. If you don't get this, make sure you've typed the program exactly as it is above.
3. Go back to the editor above and change the line int a = 2;
to int a = 3;
, then compile and run the program again.
This time the output should be c is 7
. Note that when we recompile our program, the executable file is overwritten with the new executable.
What do I need to remember?
- Programmers use two main tools: an editor to write their programs, and a terminal to compile and run their programs.
- You don't need to install any software for this course; it's all included for you in the course environment.
- Each editor window in a lesson represents a different file in the course environment.
- C is a compiled language, which means our code must first be compiled into an executable file.
- The command
ls
lists all files in the current directory (directory just means 'folder'). - The command
make
compiles our program into an executable format. For examplemake hello
to compile the filehello.c
. - The command
./
runs, or executes our programs. For example./hello
to run the executable filehello*
(don't include the asterisk). - The command
rm
deletes files. For examplerm hello
to delete the executable filehello*
(don't include the asterisk).
"First principles is a physics way of looking at the world. You boil things down to the most fundamental truths and then reason up from there." - Elon Musk