Programmers do much of their work in a terminal: a window where you type commands and a program called the shell runs them. It looks old-fashioned, but it is fast, it works the same on every server, and every tool in this course is used from it.
On Linux and macOS, open Terminal. On Windows, install Git for Windows and open Git Bash, or use WSL (Linux inside Windows). The commands below are for those shells.
Moving around
The shell always has a current folder (also called the working directory). Commands work on files in it unless you give another path.
| Command | What it does |
|---|---|
pwd | print the current folder |
ls | list the files here (ls -l adds sizes and dates) |
cd code | go into the folder code |
cd .. | go up one folder |
cd ~ | go to your home folder |
mkdir lab1 | make a new folder |
cp a.c b.c | copy a file |
mv a.c main.c | move or rename a file |
rm old.c | delete a file (there is no recycle bin!) |
cat notes.txt | print a file |
A path that starts with / is absolute: /home/ana/code/main.c. Any other path is relative to the current folder: code/main.c. A single dot . means "this folder" and .. means "the folder above". Press Tab to complete a file name, and the up arrow to bring back an earlier command.
Running a program
gcc wordcount.c -o wordcount
./wordcount
Why ./? The shell looks for commands only in a list of folders called PATH, and the current folder is not on it. ./wordcount says "the program wordcount in this folder".
Input and output
#include <ctype.h>
#include <stdio.h>
int main(void) {
int c, lines = 0, words = 0, chars = 0, inWord = 0;
while ((c = getchar()) != EOF) {
chars++;
if (c == '\n') lines++;
if (isspace(c)) {
inWord = 0;
} else if (!inWord) {
inWord = 1;
words++;
}
}
printf("%d lines, %d words, %d characters\n", lines, words, chars);
return 0;
}Input used: the cat sat ⏎ on the mat
Every program has a standard input (normally the keyboard), a standard output and a standard error (both normally the screen). The shell can connect them to files instead, and the program never knows:
| You type | What happens |
|---|---|
./wordcount < poem.txt | the program reads poem.txt as if you typed it |
./wordcount > result.txt | the output goes into result.txt (replacing it) |
./wordcount >> log.txt | the output is added to the end of log.txt |
./wordcount 2> errors.txt | only standard error goes into errors.txt |
cat poem.txt | ./wordcount | a pipe: one program's output becomes the next one's input |
This is how online judges test your code: they run it with < test.in > got.out and compare got.out with the right answer. Here the visualizer gives wordcount the input shown under the code. getchar() returns one character at a time, and EOF when the input runs out.
Careful with > and rm. > empties the file before the program even starts, and rm deletes for good. Check the file name twice.
Quick check
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.