Skip to content

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

g++ --version       # GCC
clang++ --version   # Clang (default on macOS)

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.cpp instead of g++.

The edit → compile → run loop

hello.cpp  --(compiler)-->  hello (executable)  --(run)-->  output
  1. Edit a .cpp file.
  2. Compile it: g++ -std=c++20 -Wall -Wextra hello.cpp -o hello
  3. 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:

g++ -std=c++20 -Wall -Wextra hello.cpp -o hello
./hello

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:

std::string full_name;
std::getline(std::cin, full_name);

Mixing std::cin >> x and std::getline has a trap: >> leaves the newline in the buffer, so the next getline reads an empty line. Call std::cin >> std::ws; before getline to 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::cout are 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>.
  • cout without std:: — 'cout' was not declared in this scope.
  • Running from the wrong folder — ./hello: No such file or directory. cd to where you compiled.
  • Using std::cin >> name for full names — it only reads up to the first space.

Exercises

  1. Write greet.cpp that asks for a first name and last name separately, then prints Hello, FIRST LAST!.
  2. Write rectangle.cpp that asks for a width and a height (whole numbers) and prints the area and the perimeter.
  3. Write seconds.cpp that asks for a number of seconds and prints how many whole minutes and leftover seconds that is. (Hint: / on two ints is integer division, % is remainder.)
  4. Break something: remove a semicolon, compile, and read the error. Then remove the #include <iostream> and read that error.
  5. Change return 0; to return 3;, run the program, then run echo $? (macOS/Linux) or echo %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::getline reads a line.
  • Standard library names live in the std namespace.
  • Read the first compiler error first.

Next: Variables & data types →