Skip to content
Educora
Beginner12 min1 / 10

What is C++? Your first program

Find out where C++ is used and what compiling means, then write your first program with `std::cout`.

Check yourself
In this lesson you will learn
  • Explain what C++ is used for and where
  • Describe how a compiler turns source code into a program
  • Write a program with #include <iostream>, int main() and std::cout
  • Add comments to your code

Your favourite 3D game draws dozens of frames every second, and your browser handles hundreds of page elements at once. Programs like these must be very fast, and a large share of them are written in the same language — C++. In this lesson you will find out what C++ is and how a program is born, and you will write your first one.

What is C++?

C++ is a general-purpose programming language. It was created by the Danish computer scientist Bjarne Stroustrup: the work began in 1979 under the name “C with Classes”, and in 1983 the language was renamed C++. C++ is built on top of the older and very fast C language and adds classes, templates and a rich standard library.

Definition
Compiler

A program that translates the source code a programmer writes (for example, the file hello.cpp) into machine code that the processor runs directly. The result is a separate executable file.

In languages like Python, the code is read line by line while the program runs. C++ is a compiled language: the whole code is first translated into machine code and only then run. That is why C++ programs are very fast, and many mistakes are caught at compile time, before the program even starts.

  1. 1
    Write the code

    Create a file with the .cpp extension in a text editor or an IDE (for example Visual Studio, VS Code or Code::Blocks) and type your code.

  2. 2
    Compile it

    A compiler (GCC, Clang or Microsoft Visual C++) checks the code and produces an executable file. If there is a mistake, no file is produced and you see an error message.

  3. 3
    Run it

    Run the resulting file and the program prints its result. Every time you change the code, you have to compile it again.

Terminal
g++ -std=c++17 hello.cpp -o hello
./hello
Compiling and running in a terminal with GCC. On Windows the resulting file is called hello.exe.

Where is C++ used?

  • Games and game engines — for example, the well-known Unreal Engine is written in C++.
  • Browsers and large applications — most of the Google Chrome browser is written in C++.
  • Systems and devices — parts of operating systems, drivers, robots and programs for microcontrollers such as Arduino.
  • Programming olympiads — thanks to its speed and ready-made algorithms, C++ is the most popular language among olympiad contestants.

Your first program

Programmers have a tradition: the first program in a new language prints “Hello, world!”. In C++ it looks like this:

C++
#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}
Expected output
Hello, world!
  • #include <iostream> brings in the input/output library. Without it std::cout would not work.
  • int main() is the main function where every C++ program starts. The curly braces { } mark the beginning and the end of its body.
  • std::cout (character output) is the output to the screen. The << operator “sends” a value into this stream, and std::endl ends the line.
  • The std:: prefix says that cout belongs to the standard namespace std.
  • Every statement ends with a semicolon ;. return 0; tells the operating system that the program finished successfully.

Printing several lines

A program runs its statements one by one, from top to bottom. In a single std::cout statement you can chain text, numbers and even the results of calculations with <<. To move to a new line, programmers often write the short character '\n' instead of std::endl.

C++
#include <iostream>

int main() {
    std::cout << "My name is Aysel.\n";
    std::cout << "I live in Baku.\n";
    std::cout << "I am " << 12 << " years old." << '\n';
    std::cout << "2 + 3 = " << 2 + 3 << '\n';
    std::cout << "2 + 3" << '\n';
    return 0;
}
Expected output
My name is Aysel.
I live in Baku.
I am 12 years old.
2 + 3 = 5
2 + 3
The quoted "2 + 3" is just text, while 2 + 3 without quotes is calculated.

Comments

Comments are notes for the people who read the code; the compiler ignores them. Everything from // to the end of the line is a comment. A comment spanning several lines starts with /* and ends with */.

C++
#include <iostream>

// This program greets the learner
int main() {
    /* A comment can also
       span several lines */
    std::cout << "Welcome to Educora!\n"; // greeting
    return 0;
}
Expected output
Welcome to Educora!
FeatureC++Python
How it runscompiledinterpreted
Speedvery highmoderate
Variable typesdeclared in advancedetermined automatically
Code block{ }indentation
End of a statement;end of the line
C++ compared with Python

Key points

  • C++ is a fast, compiled language used in games, browsers, system software and olympiads.
  • The compiler turns a .cpp file into an executable program; after every change you compile again.
  • Every program starts in the int main() function.
  • std::cout << ... prints to the screen; '\n' or std::endl starts a new line.
  • Statements end with ;, and comments are written with // or /* */.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
Which line do you need in order to use std::cout?