1. Setup & Your First Program¶
What & why¶
C++ is a compiled language. You write source code in a text file, a compiler translates it into a native executable, and then you run that executable. That extra step is the price of C++'s speed, and it's the first thing that trips people up. This lesson gets your toolchain working and explains the edit → compile → run loop you'll use for the rest of the course.
Check your install¶
You want GCC 13+ or Clang 17+ (or Visual Studio 2022 on Windows). If the command is not found, install a compiler (see the course overview).
On Windows with Visual Studio, open the Developer Command Prompt and use
cl /std:c++20 /EHsc /W4 hello.cppinstead ofg++.
The edit → compile → run loop¶
- Edit a
.cppfile. - Compile it:
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello - Run it:
./hello(on Windows:hello.exe)
If you change the source, you must recompile before running again. Forgetting this is the classic "my fix didn't work" bug.
The flags, explained¶
| Flag | Meaning |
|---|---|
-std=c++20 |
Use the C++20 language standard. |
-Wall -Wextra |
Turn on (most) warnings. Always use these. |
-Wpedantic |
Warn about non-standard extensions. |
-g |
Include debug info so a debugger can show your source. |
-O2 |
Optimize. Use for release builds, not while learning. |
-o hello |
Name the output file. Without it you get a.out. |
Your first program¶
Create a file called hello.cpp:
// hello.cpp — my first C++ program
#include <iostream>
#include <string>
int main() {
std::cout << "Hello, world!\n";
std::string name;
std::cout << "What's your name? ";
std::cin >> name; // reads one word
std::cout << "Nice to meet you, " << name << ".\n";
int age = 0;
std::cout << "How old are you? ";
std::cin >> age; // reads an integer directly
std::cout << "In 10 years you'll be " << age + 10 << ".\n";
return 0;
}
Compile and run it:
What each piece does¶
| Line | Meaning |
|---|---|
// ... |
A comment. The compiler ignores everything after // on that line. /* ... */ spans lines. |
#include <iostream> |
Pulls in the declarations for input/output streams (std::cout, std::cin). |
int main() { ... } |
Every program starts running at main. It returns an int exit code. |
std::cout << ... |
Writes to standard output. << chains pieces together. |
std::cin >> x |
Reads from standard input into x, converting to x's type. |
"\n" |
A newline. (std::endl also works but flushes the buffer every time — slower.) |
; |
Every statement ends with a semicolon. |
return 0; |
Exit code 0 means success. (main is the one function where you may omit it.) |
What is std::?¶
Everything in the C++ standard library lives in the namespace std. std::cout means "the cout that belongs to std". You'll see using namespace std; in many tutorials — avoid it; it pulls hundreds of names into your code and causes collisions in larger programs.
Reading a whole line¶
std::cin >> name stops at the first space. To read a full line (e.g. "Ada Lovelace"), use std::getline:
Mixing
std::cin >> xandstd::getlinehas a trap:>>leaves the newline in the buffer, so the nextgetlinereads an empty line. Callstd::cin >> std::ws;beforegetlineto skip leftover whitespace.
Compile errors vs. runtime errors¶
- Compile errors — the compiler refuses to build. Read the first error: file, line, column, message. Later errors are often caused by the first.
- Warnings — it compiled, but the compiler thinks something is fishy. Treat warnings as errors (
-Werror) once you're comfortable. - Runtime errors — it compiled but crashes or prints the wrong thing. That's what debuggers and
std::coutare for.
Common mistakes¶
- Missing semicolon — the error often points at the next line. Look one line up.
- Forgetting to recompile after editing.
- Forgetting
#include—'cout' is not a member of 'std'means you didn't include<iostream>. coutwithoutstd::—'cout' was not declared in this scope.- Running from the wrong folder —
./hello: No such file or directory.cdto where you compiled. - Using
std::cin >> namefor full names — it only reads up to the first space.
Exercises¶
- Write
greet.cppthat asks for a first name and last name separately, then printsHello, FIRST LAST!. - Write
rectangle.cppthat asks for a width and a height (whole numbers) and prints the area and the perimeter. - Write
seconds.cppthat asks for a number of seconds and prints how many whole minutes and leftover seconds that is. (Hint:/on twoints is integer division,%is remainder.) - Break something: remove a semicolon, compile, and read the error. Then remove the
#include <iostream>and read that error. - Change
return 0;toreturn 3;, run the program, then runecho $?(macOS/Linux) orecho %ERRORLEVEL%(Windows). What do you see?
Recap¶
- C++ is compiled: edit → compile → run, and recompile after every change.
- Always compile with
-std=c++20 -Wall -Wextra. - A program starts at
int main(). std::cout <<writes output;std::cin >>reads a word or number;std::getlinereads a line.- Standard library names live in the
stdnamespace. - Read the first compiler error first.
Next: Variables & data types →