C++ is a programming language used where speed matters most: game engines, web browsers, operating systems, audio software and embedded devices. It was created by Bjarne Stroustrup in the 1980s as an extension of the C language, and it's still updated every few years.
You write C++ in text files ending in .cpp. A compiler reads the whole file, checks it for errors and turns it into an executable: a program your computer can run directly. If there's a mistake, the compiler stops and tells you which line to look at.
In this lesson you'll set up a compiler, read a Hello World program line by line, and learn how to print output and write comments.
Getting a compiler
- Windows: install Visual Studio Community and choose "Desktop development with C++", or install MSYS2 to get the
g++compiler. - macOS: run
xcode-select --installin Terminal to getclang++. - Linux: install
g++with your package manager, for examplesudo apt install g++.
Check it worked:
g++ --versionAn editor such as VS Code with the C/C++ extension, or an IDE like Visual Studio or CLion, will add a Run button and highlight errors as you type.
The parts of a C++ program
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}#include <iostream>brings in the input/output library, which providescoutandcin.int main()is the main function. Every C++ program starts running here.std::coutis the standard output stream.<<sends text into it. Thestd::prefix says it comes from the standard library.std::endlends the line. You can also write"\n"inside the text.return 0;tells the operating system the program finished successfully.- Statements end with
;, and{ }mark wheremainstarts and ends.
Compiling and running
In a terminal, in the folder that holds hello.cpp:
g++ hello.cpp -o hello
./hellog++ compiles the file, and -o hello names the program it creates. ./hello runs it. On Windows the file is hello.exe, and you run it with hello or .\hello.exe. Every time you change the code, compile again before running.
using namespace std and comments
Typing std:: before every cout gets repetitive. Many beginner programs add this line after the includes:
using namespace std;Now you can write cout and endl on their own. It's fine in small programs; large projects usually keep the std:: prefix to avoid name clashes.
Comments are ignored by the compiler: // one line or /* several lines */.
What g++ does when it builds your program
The single g++ command hides three separate steps:
- Preprocessing: every
#includeline is replaced by the full text of that header. Rung++ -E hello.cppand you'll see that the short Hello World grows to tens of thousands of lines, almost all of it from<iostream>. - Compiling: the expanded code is checked and translated into machine code, stored in an object file.
g++ -c hello.cppstops here and leaves you withhello.o. - Linking: the object file is joined with the parts of the standard library it uses to make the finished executable.
g++ hello.o -o hellodoes just this step.
Knowing the steps helps you read errors. A typo or a missing ; is a compile error and comes with a line number. An error that mentions "undefined reference" comes from the linker, which means something was declared but its code was never found.
It's also worth asking the compiler for more help from day one:
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello-std=c++20 picks the language version, and -Wall -Wextra turn on warnings about code that compiles but is probably wrong.
C++ Syntax
A complete C++ program
#include <iostream>
using namespace std;
int main() {
cout << "text" << endl;
return 0;
}C++ Summary
- C++ is a fast, compiled language. Source files end in
.cpp. #include <iostream>gives youcoutfor output andcinfor input.- Every program starts in
int main(), which returns 0 on success. cout << "text" << endl;prints a line. Statements end with;.- Compile with
g++ file.cpp -o name, then run the program.
C++ Examples
Each example shows the code, the output it produces and what each part does.
Hello, world
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
std::cout << "I'm learning C++." << std::endl;
return 0;
}Hello, world!
I'm learning C++.Line-by-line explanation
- Line 1Include the library that provides cout.
- Line 3The program starts running inside main.
- Lines 4 to 5Each line sends text to cout and ends the line.
- Line 6Return 0 to say "everything went fine".
Chaining output with <<
#include <iostream>
using namespace std;
int main() {
cout << "Apples: " << 3 << ", pears: " << 5 << endl;
cout << "Total fruit: " << 3 + 5 << "\n";
cout << "Line one\nLine two" << endl;
return 0;
}Apples: 3, pears: 5
Total fruit: 8
Line one
Line twoLine-by-line explanation
- Line 2Lets us write cout and endl without std::.
- Line 5Several pieces of text and numbers can be chained with <<.
- Line 6The sum is calculated before it's printed. "\n" ends the line too.
- Line 7\n inside text starts a new line.
Integer and decimal maths
#include <iostream>
using namespace std;
int main() {
// Dividing whole numbers drops the remainder
cout << 7 / 2 << endl;
cout << 7 % 2 << endl;
cout << 7.0 / 2 << endl;
return 0;
}3
1
3.5Line-by-line explanation
- Line 5A comment. The compiler ignores it.
- Line 67 and 2 are whole numbers, so the result is a whole number: 3.
- Line 7% gives the remainder of a division.
- Line 8With a decimal number involved, the answer is a decimal.
Common C++ Mistakes
Missing semicolon
cout << "Hi" << endlcout << "Hi" << endl;The compiler reports "expected ';'" and points at the next line.
Forgetting to include iostream
int main() {
std::cout << "Hi";
}#include <iostream>
int main() {
std::cout << "Hi";
}Without the include, the compiler doesn't know what cout is.
Using >> with cout
cout >> "Hello";cout << "Hello";Arrows point towards where the data goes: << sends data out to cout, and >> reads data in from cin.
C++ in Real Projects
C++ in the real world
Unreal Engine, which powers games like Fortnite, is written in C++. So are large parts of Chrome, Firefox, Windows, Photoshop and the software in many cars and medical devices. All of those programs are, at their core, collections of functions that start from a main just like yours.
C++ FAQ
Is C++ the same as C?
No. C++ began as an extension of C, so much C code also compiles as C++, but the two are separate languages with separate standards. C++ adds classes, templates, references, exceptions and a large standard library with types like std::string and std::vector. Modern C++ code looks quite different from C, even though it can call C libraries directly.
What is the difference between endl and "\n" in C++?
Both end the line, but std::endl also flushes the output, forcing everything waiting in the buffer to be written immediately. Flushing is slow when you print thousands of lines, so "\n" is the better everyday choice. Use endl when you need output to appear straight away, for example just before a long calculation.
Which version of C++ should I learn?
Learn modern C++, meaning C++17 or C++20. New standards appear every three years (C++11, 14, 17, 20, 23), and each adds features without removing the basics. g++ 13 uses C++17 by default, so add -std=c++20 to the command to get the newer features.
Do I have to write return 0 at the end of main?
No. main is a special case: if it reaches its closing brace without a return, it returns 0 automatically. Many programmers still write return 0; to make the success value obvious. Every other function with a non-void return type must return a value.
Why does my C++ program's window close straight away on Windows?
When you double-click an .exe, Windows opens a console just for that program and closes it the moment main finishes, so the output vanishes before you can read it. Run the program from a terminal such as PowerShell or Command Prompt instead, or use your IDE's Run button, which usually keeps the window open.
C++ Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the line so it prints Ready and ends the line.
cout "Ready" << ;
What does this print?
cout << "Score: " << 4 * 5 << endl;
Which line must a program include to use cout?
What does this print?
cout << 9 / 4 << " " << 9 % 4 << endl;
What does return 0; at the end of main mean?
C++ Quiz
5 questions to check what you learned.
Where does every C++ program start running?
- At #include
- At the first cout
- In a class called Main
- In the main function
What file extension do C++ source files usually use?
- .c++
- .cxx only
- .cpp
- .h
What does
return 0;at the end of main mean?- The program finished successfully
- Print 0
- Restart the program
- The program had an error
What does the compiler do?
- Runs your code line by line
- Checks your code and turns it into a program the computer can run
- Formats your code
- Downloads libraries
Which header do you include to use cout?
- <stdio>
- <output>
- <iostream>
- <string>