Hello, World!
Your first C++ program does one thing: it shows a line of text on the screen. Every piece of code on this page compiles and runs right in your browser: press Run, change something, and run it again.
1#include <iostream>2 3int main() {4 std::cout << "Hello, World!\n";5 return 0;6}
Hello, World!
Here is what each part does:
| Part | What it is |
|---|---|
#include <iostream> | Brings in the tools for input and output. std::cout is one of them. |
int main() | The program starts running at main. Every C++ program has one. |
{ } | The curly braces hold what main does. |
std::cout << "..."; | Sends the text in quotes to the screen. \n is a new line. |
return 0; | Tells the system the program finished without trouble. |
What compiling is
C++ is a compiled language: your code first goes to a compiler (g++, for example), which translates it into a program the computer can run directly. Only then does it run. Here, when you press Run, the page compiles it for you, with the same results as g++.
The compiler is strict. Leave out one semicolon and it refuses to compile, and tells you what is wrong:
1#include <iostream>2 3int main() {4 std::cout << "No semicolon\n"5 return 0;6}
main.cpp: In function 'int main()':
main.cpp:4:34: error: expected ';' before 'return'
4 | std::cout << "No semicolon\n"
| ^
| ;
5 | return 0;
| ~~~~~~Read the line with error: in it: main.cpp:4:34 is the file name, line 4, column 34. After error: comes what is wrong. The compiler even points to where the ; belongs. Fix it and run it again.
Tip: C++ error messages can get long. Read the first error: first; the ones after it are often caused by it.
Line by line
The statements in main run from top to bottom, one after another, and each one ends with a semicolon. Press Step to watch it go line by line.
1#include <iostream>2 3int main() {4 std::cout << "First line\n";5 std::cout << "Second line\n";6 std::cout << "Third line\n";7 return 0;8}
First line Second line Third line
cout and <<
std::cout is the output stream: whatever you send to it with << goes to the screen. One statement can send several things in a row, and numbers work too:
1#include <iostream>2 3int main() {4 std::cout << "Apples: " << 3 << "\n";5 std::cout << "Total: " << 3 + 4 << "\n";6 return 0;7}
Apples: 3 Total: 7
std::cout does not start a new line by itself. Without \n, the next output carries on along the same line:
How many lines does this show?
1#include <iostream>2 3int main() {4 std::cout << "A";5 std::cout << "B\n";6 std::cout << "C\n";7 return 0;8}
"A" has no \n, so B comes right after A: it shows AB, then a new line. Then C gets a line of its own. Two lines in all.
You will also see std::endl in other code. It ends the line too (and flushes the output right away). This course writes \n (from lesson 2 on as '\n'), which is enough almost everywhere.
What std:: means
cout belongs to the standard library, whose names all live in std. std::cout means "the cout from std". Leave out std:: and the compiler doesn't know the name:
1#include <iostream>2 3int main() {4 cout << "Hi\n";5 return 0;6}
main.cpp: In function 'int main()':
main.cpp:4:5: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
4 | cout << "Hi\n";
| ^~~~
| std::coutg++ even suggests the fix: did you mean 'std::cout'?. Many tutorials write using namespace std; at the top so they can leave std:: out. This course writes std:: every time: in bigger programs it avoids clashes between names, and you always see where a name comes from.
Special characters
Inside a string, a backslash \ with the character after it stands for a character that is hard to type directly:
| You write | You get |
|---|---|
\n | a new line |
\t | a tab (jumps to the next column stop) |
\" | a double quote |
\\ | a backslash |
1#include <iostream>2 3int main() {4 std::cout << "Name\tScore\n";5 std::cout << "Ana\t95\n";6 std::cout << "She said \"hi\"\n";7 std::cout << "Path: C:\\code\n";8 return 0;9}
Name Score Ana 95 She said "hi" Path: C:\code
Comments
Comments are for people; the compiler skips them. From // to the end of the line is a comment, and so is everything between /* and */, even across several lines.
1#include <iostream>2 3int main() {4 // say hello first5 std::cout << "Hello\n";6 /* not this one for now:7 std::cout << "Goodbye\n";8 */9 return 0;10}
Hello
main and return 0
The program starts at the first statement in main and ends at return 0;. The 0 means "all went well"; other programs can see this number and tell whether it went wrong. Every program in this course is written this way, with return 0; at the end of main.
1#include <iostream>2 3int main() {4 std::cout << "Start\n";5 return 0;6 std::cout << "This line never runs\n";7}
Start
The program ends at return, so the last line shows nothing.
Practice
Write the code, then press Check. A hint costs nothing; the answer is there when you need it.
Your first program
✓Make the program show Hello, C++!, then a new line.
Hello, C++!
1#include <iostream>2 3int main() {4 5 return 0;6}
One statement, two lines
✓Use only one std::cout statement to show these two lines.
Apples Bananas
1#include <iostream>2 3int main() {4 std::cout << "Apples";5 return 0;6}
Fix it: a missing name
✓This program doesn't compile. Fix it so it shows Price: 25.
Price: 25
1#include <iostream>2 3int main() {4 cout << "Price: " << 25 << "\n";5 return 0;6}
What you learned
- A C++ program starts running at
main;#include <iostream>brings instd::cout. - C++ is compiled: first it is compiled, then it runs. A compile error gives the line and the reason; read the first one first.
std::cout << ...shows text and numbers; several<<in a row show several things.- Output doesn't start new lines by itself: write
\n.\t,\"and\\stand for special characters. - Names from the standard library start with
std::;//and/* */are comments;return 0;means all went well.