C++ Foundations

Lesson 1 of 6

Hello, C++

C++ is a compiled, statically-typed language, your source code is translated into machine code by a compiler before the program ever runs. That extra step is part of why C++ programs run so fast, and why "compile errors" catch many mistakes before you even try running anything. It's used everywhere performance matters: games and game engines, operating systems, embedded devices, and other high-performance software.

Your first program

#include <iostream>

int main() {
    std::cout << "Hello, Kodstigen!" << std::endl;
    return 0;
}

Reading it line by line

  • #include <iostream> pulls in the input/output library, without it std::cout wouldn't exist.
  • int main() { ... } is the entry point, every C++ program starts running here, and only here.
  • std::cout << "..." sends text to the console, std:: means "this comes from the standard library", and << pushes a value into the output stream.
  • std::endl ends the current line.
  • return 0; tells the operating system the program finished successfully, by convention 0 means "no errors" (anything else signals a problem).

Compiling and running

Unlike Python or JavaScript, C++ code doesn't run directly, a compiler translates it into an executable first:

g++ hello.cpp -o hello
./hello

g++ hello.cpp -o hello compiles hello.cpp into a program named hello, ./hello then runs it.

📝 C++ Intro Quiz

Passing score: 70%
  1. 1.Which function is the entry point of every C++ program?

  2. 2.C++ source code must be compiled before it can run.