A program is a list of instructions for a computer, written as text. C++ is a programming language: a set of rules for writing those instructions. To run a program is to ask the computer to carry the instructions out.
A computer cannot follow C++ text directly. A tool called a compiler first reads the whole text, checks that it obeys the rules of the language and translates it into a form the computer can run. If the compiler finds text that breaks the rules, it refuses and tells you where. Only a program that compiles can run.
You do not need to install anything for these lessons. Every example with an Edit & run button is compiled and run right here on the page, and you can change it and run it again. Nothing you type can break anything.
The smallest useful program
Here is a complete C++ program. Run it.
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
}It shows Hello, world! on the screen. The text a program shows is called its output.
Only the fourth line does the visible work. The other lines are a frame that every program in this unit has, so you can copy them each time:
#include <iostream>brings in the part of C++ that handles input and output. Without it the compiler does not know whatstd::coutis.int main() {and the closing}on the last line mark where the program's instructions go. Running a C++ program means carrying out the instructions between those two curly brackets, called braces. Lesson 6 explains what the wordsintandmain()mean; until then, treat this line as fixed.
Now the fourth line, piece by piece:
std::coutis the screen. The name is short for "character output", andstd::in front says that it belongs to the standard library: the collection of ready-made tools that comes with C++.<<sends the value on its right to the thing on its left. Read it as "send to". The arrows point the way the text travels: towards the screen."Hello, world!\n"is a string: a piece of text. The quotation marks tell the compiler where the text starts and stops. They are not part of the text, so they are not shown.\ninside a string means "start a new line". The backslash and the n are not shown; the screen moves to the next line instead.;ends the instruction, the way a full stop ends a sentence.
Statements run from top to bottom
A statement is one complete instruction. In C++ a statement ends with a semicolon. A program with three statements does three things, always in the order they are written: first statement first.
#include <iostream>
int main() {
std::cout << "Fill the kettle\n";
std::cout << "Boil the water\n";
std::cout << "Make the tea\n";
}This prints three lines, in the same order as the statements:
Fill the kettle
Boil the water
Make the teaStep through the diagram to watch each statement run. Then swap two of the statements in the program above and run it again: the output changes order too, because the computer never looks ahead or rearranges your statements.
\n, so each one finishes its line on the screen.A new line happens only where you ask for one
std::cout does not start a new line by itself. It shows exactly the characters you send, one after another. The line ends only where a \n appears.
#include <iostream>
int main() {
std::cout << "Tea";
std::cout << " and cake\n";
std::cout << "Thank you\n\n";
std::cout << "Goodbye\n";
}This prints:
Tea and cake
Thank you
GoodbyeThe first two statements together make one line, because the first string has no \n. The two \n characters after Thank you end that line and then make an empty one.
You can also send several things in one statement by repeating <<. This statement prints the same first line:
std::cout << "Tea" << " and cake\n";In other people's programs you will often see std::endl used to end a line, as in std::cout << "Tea" << std::endl;. It also ends the line, but in addition it forces the output to be delivered to the screen immediately every time, which makes programs that print a lot slower for no benefit. This unit ends lines with \n.
Comments are notes for people
A comment starts with //. The compiler ignores everything from the // to the end of that line. Comments are for the person reading the program, including you next week.
#include <iostream>
int main() {
// Show a two-line welcome.
std::cout << "Welcome!\n";
std::cout << "Come in.\n"; // the last line
}This prints Welcome! and then Come in.. The comments print nothing.
A // inside the quotation marks of a string is ordinary text, not a comment: std::cout << "See // below\n"; prints See // below.
Exactness matters
The compiler follows your text exactly, so small differences matter:
- Capital letters count.
std::coutworks;std::Coutis a different name that the compiler does not know. - Everything between the quotation marks is shown exactly, including spaces and punctuation.
"Hello,world!"and"Hello, world!"are different strings. - Each string needs a quotation mark at both ends, each statement needs its semicolon, and each opening brace
{needs a closing brace}.
Spaces and line breaks between the pieces of a statement are up to you. The statements inside main are moved four spaces to the right, which is called indenting them. The compiler does not care about indenting, but people do: it shows at a glance which lines are inside the braces.
Read your first error message
Everyone who programs sees error messages every day. An error message is not a telling-off. It is the compiler saying which piece of text it could not understand, and why. Learning to read one is a skill in its own right.
This program has a mistake: the semicolon at the end of line 4 is missing.
#include <iostream>
int main() {
std::cout << "Hello, world!\n"
}The compiler refuses to translate it and reports:
main.cpp: In function 'int main()':
main.cpp:4:35: error: expected ';' before '}' token
4 | std::cout << "Hello, world!\n"
| ^
| ;
5 | }
| ~Read it from the top down:
- Find the line that contains the word
error. Here it saysexpected ';' before '}' token: the compiler wanted a semicolon and met a closing brace instead. (A "token" is one piece of program text, such as a name, a bracket or a string.) - Read the position in front of it.
main.cpp:4:35means the filemain.cpp, line 4, character 35 along that line. On this page your program is always calledmain.cpp. - Look at the picture underneath. The compiler repeats line 4 of your program and puts a
^under the exact spot, then shows what it suggests adding there:;.
The very first line, In function 'int main()':, only says which part of the program the problem is in. Here that is main, the only part this program has.
A second very common mistake is a misspelt name:
#include <iostream>
int main() {
std::cot << "Hello, world!\n";
}main.cpp: In function 'int main()':
main.cpp:4:10: error: 'cot' is not a member of 'std'; did you mean 'oct'?
4 | std::cot << "Hello, world!\n";
| ^~~
| oct"cot is not a member of std" means the standard library has nothing called cot. The position is line 4, character 10, and the ^~~ marks sit under the unknown name. The compiler also guesses what you meant, and this time the guess is wrong: oct is a real name in the standard library, but you wanted cout. Treat a suggestion as a hint about where to look, never as an instruction.
When a program has several mistakes, the compiler may print many messages. Fix the first one and compile again: later messages are often only consequences of the first.
In the exercises: program instead of main
After this reading come exercises that check your code automatically: two broken programs to repair, a program to complete and a program to write yourself. To check a program, the site's tests run it several times and read what it sends to std::cout. The tests need a main of their own to do that, and a C++ program may contain only one main. So in those exercises your instructions go between the braces of program instead:
#include <iostream>
void program() {
std::cout << "Hello, world!\n";
}Write between those braces exactly what you would write inside main. The first word is void where main has int; lesson 6 explains both. Nothing else changes.
In those exercises the compiler calls your file solution.hpp where this page says main.cpp. Its messages may also begin with a line such as In file included from main_test.cpp:2:, which belongs to the tests and can be ignored. Read on to the first line that mentions solution.hpp.
Running C++ on your own computer
This is optional; the lessons do not need it. To run C++ yourself you install a compiler, for example GCC or Clang (the Get Started page of the C++ Foundation lists free ones for each system). You save a program in a text file named hello.cpp, open a terminal (a window where you type commands) in the same folder, and type g++ hello.cpp -o hello to compile it. If there are no errors, typing ./hello runs it (on Windows, hello.exe), and the output appears in the terminal, the same as it does on this page.
What you can do now
You can write a program that sends several lines of text to the screen, predict the order and the line breaks of its output, add comments, and find the place an error message is pointing at. In the exercises you will predict what small programs print, repair two broken ones and then write a three-line program of your own.
Next: name values with variables, so that a program can remember and calculate things.