Your First Program

Welcome to the C++ course! C++ is one of the most widely used programming languages in the world. Computer games and game engines such as Unreal Engine are written in it, as are web browsers, photo and video editors, and the software in cars, robots and many other devices. Programmers choose it wherever a program has to run fast.

In this course, you'll learn to write programs that calculate, make decisions, repeat work, work with lists of values and are built from smaller parts called functions. At the end, you'll write a small project of your own. And you'll write your first program right now.

A program is a series of statements: commands that the computer carries out one after another, from top to bottom. But a computer doesn't understand C++ directly. First, a compiler has to process the program: it checks that the program is written correctly and translates it into instructions that the computer understands. When you click Run, this site compiles your program and runs it straight away.

This is a whole program that prints a greeting:

#include <iostream>

int main() {
    std::cout << "Hello!" << std::endl;
}

This prints:

Hello!

What the parts mean:

  • #include <iostream> adds a part of the standard library to the program: the set of ready-made tools that always comes with C++. This part takes care of input and output, and it can print text. Without it, the program wouldn't know std::cout.
  • int main() { } is the main part of the program, the main function. This is where the program starts: it carries out the statements between the curly braces { and } from top to bottom, and then it ends. Every C++ program has exactly one main function. You'll find out what int and the parentheses () mean in the lesson on functions. For now, take it as a fixed frame that you write your code into.
  • std::cout (character output) is the program's output. Whatever you send to it appears in the output. std:: means "from the standard library". Online, you'll often see programs that start with the line using namespace std; and then write just cout. It saves typing, but in bigger programs, names can then get mixed up, so in this course, we write std::.
  • << sends what follows it to the output. You can think of it as an arrow pointing to std::cout.
  • "Hello!" is text. Text goes in double quotes ", which show where the text starts and where it ends. Exactly what is between them gets printed.
  • std::endl (end line) ends the line, so the next output starts on a new line.
  • The semicolon ; ends a statement, like a full stop ends a sentence. It goes after every statement inside main. The lines #include, int main() { and } aren't statements, so they don't have one.

C++ treats uppercase and lowercase letters as different, so std::Cout ends with an error. When a program is missing a semicolon, for example, the compiler doesn't run it and shows a red error message in the output, such as expected ';' after expression. Don't worry about it: check the semicolons and quotes, and run the program again. Later in this lesson, you'll learn to read these messages.

Instructions

The editor contains the program from the example. Click Run below the editor and look at the output (on a phone, under Output). The first run may take a few seconds, while the compiler loads.

Then change the text in the quotes, so that the program prints the traditional first message of every programmer:

Hello, world!

Below this statement, add a second std::cout statement that prints your name on the next line. Don't forget the quotes and the semicolon at the end.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/6