All lessons

Your First Program

6 min · C++

Write and run your very first C++ program, right here on the page.

C++ · Your First Program0/5 cleared

Enemy 1 of 5

What a C++ program looks like

Every C++ program needs a starting point, and that starting point is always a function named main. When you click Run, the computer doesn't read your file top to bottom like a story. It jumps straight to main and begins executing the instructions inside its curly braces { }. Everything the program actually does lives inside that block.

Before main, you'll usually see one or more #include lines. These pull in pieces of the C++ standard library so you can use tools that aren't built into the bare language. In the example below, #include <iostream> brings in input/output support. Without it, cout (which prints to the screen) wouldn't exist at all. Think of #include as saying "I'm going to need this toolbox, please hand it to me before I start."

The line using namespace std; tells the compiler it's fine to write cout instead of the fully-qualified std::cout. It's a convenience that most beginner (and plenty of professional) programs use to keep code shorter and easier to read.

You
Binary Treant
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.

Stuck? Ask Rena

Your AI tutor that gives assistance.

Asking Rena in lessons is on Ultimate.