C++ Introduction

What C++ is, how to install a compiler, and how a Hello World program works line by line.

Lesson 1 of 44Beginner18 min read

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 --install in Terminal to get clang++.
  • Linux: install g++ with your package manager, for example sudo apt install g++.

Check it worked:

Terminal
g++ --version

An 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

C++
#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}
  • #include <iostream> brings in the input/output library, which provides cout and cin.
  • int main() is the main function. Every C++ program starts running here.
  • std::cout is the standard output stream. << sends text into it. The std:: prefix says it comes from the standard library.
  • std::endl ends 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 where main starts and ends.

Compiling and running

In a terminal, in the folder that holds hello.cpp:

Terminal
g++ hello.cpp -o hello
./hello

g++ 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:

C++
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:

  1. Preprocessing: every #include line is replaced by the full text of that header. Run g++ -E hello.cpp and you'll see that the short Hello World grows to tens of thousands of lines, almost all of it from <iostream>.
  2. Compiling: the expanded code is checked and translated into machine code, stored in an object file. g++ -c hello.cpp stops here and leaves you with hello.o.
  3. Linking: the object file is joined with the parts of the standard library it uses to make the finished executable. g++ hello.o -o hello does 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:

Terminal
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 you cout for output and cin for 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.

Example 1

Hello, world

C++
#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    std::cout << "I'm learning C++." << std::endl;
    return 0;
}
Output
Hello, world!
I'm learning C++.
Line-by-line explanation
  1. Line 1Include the library that provides cout.
  2. Line 3The program starts running inside main.
  3. Lines 4 to 5Each line sends text to cout and ends the line.
  4. Line 6Return 0 to say "everything went fine".
Example 2

Chaining output with <<

C++
#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;
}
Output
Apples: 3, pears: 5
Total fruit: 8
Line one
Line two
Line-by-line explanation
  1. Line 2Lets us write cout and endl without std::.
  2. Line 5Several pieces of text and numbers can be chained with <<.
  3. Line 6The sum is calculated before it's printed. "\n" ends the line too.
  4. Line 7\n inside text starts a new line.
Example 3

Integer and decimal maths

C++
#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;
}
Output
3
1
3.5
Line-by-line explanation
  1. Line 5A comment. The compiler ignores it.
  2. Line 67 and 2 are whole numbers, so the result is a whole number: 3.
  3. Line 7% gives the remainder of a division.
  4. Line 8With a decimal number involved, the answer is a decimal.

Common C++ Mistakes

Missing semicolon

Don't
cout << "Hi" << endl
Do
cout << "Hi" << endl;

The compiler reports "expected ';'" and points at the next line.

Forgetting to include iostream

Don't
int main() {
    std::cout << "Hi";
}
Do
#include <iostream>

int main() {
    std::cout << "Hi";
}

Without the include, the compiler doesn't know what cout is.

Using >> with cout

Don't
cout >> "Hello";
Do
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.

Fill in the blankseasy

Complete the line so it prints Ready and ends the line.

cout  "Ready" << ;
Predict the outputeasy

What does this print?

cout << "Score: " << 4 * 5 << endl;
Multiple choicemedium

Which line must a program include to use cout?

Predict the outputmedium

What does this print?

cout << 9 / 4 << " " << 9 % 4 << endl;
Multiple choicemedium

What does return 0; at the end of main mean?

C++ Quiz

5 questions to check what you learned.

  1. Where does every C++ program start running?

    • At #include
    • At the first cout
    • In a class called Main
    • In the main function
  2. What file extension do C++ source files usually use?

    • .c++
    • .cxx only
    • .cpp
    • .h
  3. What does return 0; at the end of main mean?

    • The program finished successfully
    • Print 0
    • Restart the program
    • The program had an error
  4. 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
  5. Which header do you include to use cout?

    • <stdio>
    • <output>
    • <iostream>
    • <string>