The terminal

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.

CommandWhat it does
pwdprint the current folder
lslist the files here (ls -l adds sizes and dates)
cd codego into the folder code
cd ..go up one folder
cd ~go to your home folder
mkdir lab1make a new folder
cp a.c b.ccopy a file
mv a.c main.cmove or rename a file
rm old.cdelete a file (there is no recycle bin!)
cat notes.txtprint 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

Open in compiler

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 typeWhat happens
./wordcount < poem.txtthe program reads poem.txt as if you typed it
./wordcount > result.txtthe output goes into result.txt (replacing it)
./wordcount >> log.txtthe output is added to the end of log.txt
./wordcount 2> errors.txtonly standard error goes into errors.txt
cat poem.txt | ./wordcounta 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

1. You are in /home/ana/code. Where does cd .. take you?
2. What does ./app < in.txt > out.txt do?
3. Why do you type ./app and not just app?

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.