Chapter 1 installs a compiler and runs it. You can do most of that by
clicking, but every C++ programmer eventually types commands into a
terminal, and the course will show you both. Chapter 1 also assumes
you can save a file of plain text under the name you meant. This
section covers both. You'll open a terminal, look around, make a folder
for the course, and save your first .cpp file in it.
A file is one saved thing on your computer: a photo, a song, a
letter. Its name usually ends with a dot and a few letters, like
.jpg or .txt. That ending is the extension, and it tells the
computer what kind of file it is. C++ code goes in a file whose name
ends in .cpp.
That file must be plain text: only the letters you typed, nothing else. A word processor like Word saves more than your words. It also saves fonts, sizes and page layout, mixed in with the text. The compiler can't read that mix.
Files live in folders, and folders live inside other folders. A folder
path is a street address for a file. C:\Users\Ada\cpp-course reads
like "drive C, then the Users folder, then Ada's folder, then the
cpp-course folder inside it." A full address like that works from
anywhere. A short one like cpp-course means "the cpp-course folder
right here," so it only works when you're standing in the right place.
The terminal is a text conversation with your computer. You type an instruction, called a command, and press Enter. The computer does it and types back the result. At any moment the terminal is "standing in" one folder, the current directory. When you say "show me the files," it shows that folder's files.
Commands and paths are different things. A command is a verb, like
cd, which means "change directory." A path is a place, like
cpp-course. You usually give a command a path to work on: cd cpp-course means "go into the cpp-course folder."
A file is a named sequence of bytes stored on a disk. Its name
usually ends in an extension: a dot and a few letters (.cpp,
.txt, .exe). The extension tells the operating system and other
programs what the bytes are meant to be. A plain-text file holds
only the codes of the characters typed into it, so any text editor can
open it and a compiler can read it. A word processor's file (.docx,
.rtf, .pages) also stores fonts and layout. A compiler can't read
that format.
A file system organizes files into nested directories (folders).
A path names one file or directory by listing the directories you
pass through to reach it: C:\Users\Ada\cpp-course on Windows,
/Users/ada/cpp-course on macOS, /home/ada/cpp-course on Linux. The
first two use \ and / as separators respectively. Windows also names
drives with letters (C:). An absolute path like these starts at
the top of the file system and means the same place from anywhere. A
relative path, such as cpp-course/ch01, starts from the current
directory (below). ~ stands for your home directory and .. for the
directory that contains the current one.
A terminal (also called a console or command prompt) runs a shell, a program that reads a typed command, runs it, and prints the result. Every shell has a current directory: relative paths and commands like "list the files" apply to that directory. PowerShell is the default shell on Windows. zsh (macOS) and bash (most Linux distributions) are the defaults elsewhere.
Open a terminal and make a course folder
On Windows 11, press the Start key, type Terminal, and press Enter. On Windows 10, type PowerShell instead. Either way you get a PowerShell window. Then type these one at a time, pressing Enter after each:
cd ~
mkdir cpp-course
cd cpp-course
pwd
~ is your home folder, C:\Users\<you>. pwd ("print working
directory") shows where you're standing now:
C:\Users\<you>\cpp-course.
PowerShell is chattier than the other shells. mkdir prints a small
table about the folder it made (Directory:, then Mode,
LastWriteTime and Name columns), and pwd prints the word Path
and a line of dashes above the path. That's normal, not an error.
Press Cmd+Space, type Terminal, and press Enter. Then type these one at a time, pressing Enter after each:
cd ~
mkdir cpp-course
cd cpp-course
pwd
~ is your home folder, /Users/<you>. pwd ("print working
directory") shows where you're standing now: /Users/<you>/cpp-course.
Open your applications menu and search for Terminal (or press Ctrl+Alt+T on most desktops). Then type these one at a time, pressing Enter after each:
cd ~
mkdir cpp-course
cd cpp-course
pwd
~ is your home folder, /home/<you>. pwd ("print working
directory") shows where you're standing now: /home/<you>/cpp-course.
The same three commands, cd, mkdir, and ls (list files), work in
PowerShell, zsh, and bash. This course leans on them for that reason.
Command or path?
Sort each card: is it a command (an instruction to do something) or a path (the address of a folder or file)?
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
cd, mkdir cpp-course, ls, and pwd are instructions. The three addresses name a folder: one Windows-style, one macOS-style, and one using ~ as shorthand for the home folder.Worked example
Where am I, and what's here?
-
1
Start fresh: open a terminal and type
pwd. It prints your home folder. That's where every new terminal starts. -
2
Type
cd cpp-course. Nothing is printed, in any of the three shells. Quiet means success. Typepwdagain and you'll see the path now ends incpp-course. -
3
Type
ls. The folder is empty, so nothing is listed. In a minute you'll save a file here. Thenlswill show it. -
4
Type
cd ..(two dots)...always means "the folder that contains this one," so you're back in your home folder.
Worked example
Two ways to name the same folder
-
1
You're in your home folder.
cd cpp-courseworks:cpp-courseis a relative path, so the terminal looks for it inside the folder you're standing in. -
2
Now
cdinto some other folder, saycd Documents, and trycd cpp-courseagain. It fails with a message likeno such file or directory(PowerShell saysCannot find path). There's no cpp-course inside Documents. -
3
cd ~/cpp-courseworks from anywhere. The~makes it start at your home folder every time. A full path such as/Users/ada/cpp-courseorC:\Users\Ada\cpp-courseworks from anywhere too. That's an absolute path.
Set up a folder for Chapter 1
Put the commands in order: start from your home folder, make a folder called ch01 inside cpp-course, go into it, and list its contents.
Loading interactive…
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
What this task checks
Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.
The compiler says
A model answer
Correct answer
Also accepted:
Why
Why
cpp-course before mkdir ch01 creates it there. Then cd ch01 enters the new folder, and ls lists what's inside (nothing yet).Practice: Find your way around the terminal (5 problems · doesn’t affect your progress)
Which of these is a relative path?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
A path that starts with / begins at the top of the file system, so it means the same place from anywhere.
A drive letter such as C: pins the path to the top of that drive. It's absolute.
~ stands for your home folder, so this path starts there no matter where you're standing.
Why
cpp-course/ch01 has no /, drive letter or ~ in front, so the terminal looks for it inside the current directory.
An absolute path starts from a fixed place: a /, a drive letter, or ~. Which one starts from none of those?
cpp-course/ch01
Which command lists the files in the current folder?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
cd moves you to another folder. It doesn't list anything.
mkdir creates a folder.
pwd prints where you are, not what's there.
Why
ls lists the current directory's contents. PowerShell accepts ls too.
You want to see what's inside the folder, not move, create or print a path.
ls
You type mkdir notes and then ls. What do you see?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
New folders aren't hidden.
ls lists the current folder, and you're still in the one that now contains notes.
You never moved into notes, so ls shows the folder that contains it.
Why
mkdir creates notes inside the current folder without moving you, so ls lists it alongside anything already there.
Does mkdir move you into the new folder, or leave you where you were?
notes, plus anything else that was already in the folder
What two characters mean the folder that contains the current one?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Why
.. is the parent folder, so cd .. moves up one level.
Correct answer
Also accepted:
Why
It's the same shorthand that cd uses to go up one level.
You're in /Users/ada. You type cd cpp-course/ch01, then cd .. twice, then pwd. What does pwd print?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
That's after one cd ... The second one climbs again, from cpp-course to ada.
That's three levels above ch01. Two cd .. commands climb two levels.
Each cd .. moves you up, so you can't still be in ch01.
Why
The first cd goes two folders down, into /Users/ada/cpp-course/ch01. Each cd .. goes up one: first to /Users/ada/cpp-course, then back to /Users/ada.
Track the path after each command: one cd goes down two folders, and each cd .. climbs one.
/Users/ada
Save your first plain-text file
A C++ file must be plain text, and its name must end in .cpp with
nothing after it.
Three things get in the way, and all are invisible until you know to
look. A word processor, or TextEdit in its default mode, saves fonts
and layout along with your words. Windows and macOS hide extensions
by default, so a file whose real name is first.cpp.txt shows up as
first.cpp. And many editors have a "smart quotes" feature. It swaps
the straight " you type for a curly “ or ”. C++ only accepts the
straight one, so curly quotes make the compiler reject your code. Word
processors and macOS TextEdit both do this unless you turn it off.
Notepad and nano never do. The steps below turn all three problems off. The file holds one
line, // my first C++ file. In C++, // starts a comment: a note
for people that the compiler skips.
- Make extensions visible. Open File Explorer. On Windows 11, choose View → Show → File name extensions. On Windows 10, open the View tab and tick File name extensions. Now every file shows its real, full name.
- Open Notepad (press Start and type Notepad). Notepad saves plain
text. Type
// my first C++ file. - Choose File → Save as. Go to your
cpp-coursefolder, insideC:\Users\<you>. Set Save as type to All files (*.*). With the type left at "Text documents (*.txt)", Notepad can add.txtto the name. Type the namefirst.cppand save. - Check in the terminal:
cd ~\cpp-course
ls
The list shows first.cpp. If it shows first.cpp.txt, rename it with
mv first.cpp.txt first.cpp.
- Open TextEdit. A new document starts as rich text, which saves fonts and styles along with your words. Choose Format → Make Plain Text. To make every new document plain text, set it in TextEdit → Settings → New Document → Plain text.
- Choose Edit → Substitutions and turn off Smart Quotes.
Otherwise TextEdit swaps a typed
"for a curly quote, which C++ doesn't accept. - Type
// my first C++ file. Choose File → Save, name itfirst.cpp, and pick thecpp-coursefolder in your home folder. If TextEdit asks whether to use.cppor.txt, choose.cpp. - Check in the terminal:
cd ~/cpp-course
ls
The list shows first.cpp. If it shows first.cpp.txt, rename it with
mv first.cpp.txt first.cpp. Finder hides extensions too. To see them,
turn on Finder → Settings → Advanced → Show all filename extensions.
- The quickest editor is inside the terminal. Type
nano first.cppand press Enter. nano opens an empty file with that name. - Type
// my first C++ file. Press Ctrl+O and then Enter to save, and Ctrl+X to leave nano. (Your desktop's Text Editor app also saves plain text, if you'd rather click.) - Check:
ls
The list shows first.cpp. Linux file managers and ls always show
the full name, extension included.
Worked example
The file that isn't there
-
1
In Chapter 1 you'll save
hello.cppand rung++ hello.cpp -o hello. Suppose the compiler answershello.cpp: No such file or directory. But you can seehello.cppin File Explorer or Finder. -
2
Ask the terminal, which never hides anything:
ls. It listshello.cpp.txt. The editor added.txt, and the file browser was hiding it. Your file has a different name from the one you typed in the command. -
3
Rename it,
mv hello.cpp.txt hello.cpp, and run the compile command again. Iflsshows no hello file at all, you're in the wrong folder: check withpwd, thencdto the folder where you saved it.
Where this goes next
Chapter 1
Chapter 1 §2 installs a compiler and checks it with g++ --version.
If the terminal says command not found, that's this section's idea
again: the shell looks for commands in a list of folders called PATH,
and the compiler's folder isn't on the list yet. Chapter 1 §3 saves
hello.cpp as plain text in your cpp-course folder, compiles it
there, and runs the result as ./hello on macOS and Linux. ./hello
is a relative path: "the program called hello, in the folder I'm
standing in."
Who uses this
Software development
Build servers, cloud machines, and the tools that compile big C++ projects have no mouse at all. They run the same kind of commands you just typed. Professional C++ developers who mostly use an IDE still drop into a terminal to run a build, a test, or a quick search. It's a permanent part of the job.
Check your understanding (counts toward your progress)
cd cpp-course works in your home folder but fails in your Documents folder. cd ~/cpp-course works in both. Why?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Nothing in Documents blocks commands. The folder you named just isn't inside it.
You can use cd as often as you like.
~ never creates anything. It's shorthand for your home folder, which already holds cpp-course.
Why
A relative path like cpp-course is looked up inside the current directory, and Documents has no cpp-course in it. ~/cpp-course starts at your home folder, so it names the same place from anywhere.
Where does the terminal look for a name that doesn't start with ~ or a drive or a slash?
Which of these is a path, not a command?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
pwd is a command: print working directory.
mkdir is a command: make directory.
ls is a command: list the files here.
Why
/home/ada/labs is an address: the labs folder inside ada's home folder on Linux. The others are instructions.
What does the command cd cpp-course do?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Creating a folder is mkdir, short for make directory.
cd never deletes anything. It only changes where the terminal is standing.
Listing files is ls. cd changes the current directory.
Why
cd means change directory. After cd cpp-course, the terminal's current directory is cpp-course.
You're standing in /Users/ada/cpp-course/ch01. You type cd ... Where are you now?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
.. goes up exactly one level, not two.
The terminal resolves .. for you. pwd would show the parent folder's real path.
.. is real in every folder: it always means the folder that contains this one.
Why
.. means the folder containing the current one, so cd .. goes up one level, from ch01 to cpp-course.
.. always means one level up: the folder that contains this one.
Practice: Save a file the compiler can read (4 problems · doesn’t affect your progress)
Which of these files can a C++ compiler read as source code?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
A Word file stores fonts and page layout mixed in with the words. The compiler can't read that mix.
Rich text stores fonts and styles too. TextEdit needs Format → Make Plain Text first.
A PDF holds a laid-out page, not plain characters.
Why
Source code must be plain text: only the characters you typed. Notepad saves plain text, and the .cpp ending marks it as C++.
The compiler reads only the characters you typed. Which file holds nothing extra, like formatting or layout?
first.cpp, saved from Notepad as plain text
Why must an editor's smart-quotes feature be off when you write C++?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
A curly quote is one character, like a straight one. The problem is which character it is, not the file size.
Adding .txt comes from the save type, not from smart quotes.
Hiding extensions is a file-browser setting. Smart quotes change the characters you type.
Why
C++ only accepts the straight " character around text. Smart quotes replace it with a curly one as you type, and the compiler rejects that.
Look closely at the quote marks C++ accepts around text. What do smart quotes do to them as you type?
It swaps a typed " for a curly quote, which C++ doesn't accept
ls lists first.cpp.txt. Type the command that renames it to first.cpp.
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Why
mv first.cpp.txt first.cpp renames the file. The old name comes first and the new name second.
Correct answer
Also accepted:
Why
The command is mv, followed by the old name and then the new name.
The compiler says hello.cpp: No such file or directory. You run ls, and it lists no hello file at all, not even hello.cpp.txt. What's the likely cause?
Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.
Then ls would list hello.cpp.txt. It listed no hello file at all.
A file with the wrong contents would still show up in ls, and the compiler would complain about its contents, not its existence.
A missing compiler gives a command not found message, not a complaint about hello.cpp.
Why
ls shows only the current directory, so the file must be somewhere else. Check with pwd, then cd to the folder where you saved it.
ls lists only the current folder. Which folder is that?
The terminal is in a different folder from the one you saved to