Shiklo Tools
PDF40
Image20
Calculators & Converters25
Text & Writing13
Developer26
Video14
Audio14
Document14
Data & Spreadsheet12
Web & SEO9
OCR & Scanning5
Archive & File12
Security & Privacy23
Health & Medical29
QR & Barcode5

C++ Get Started

A C++ program is written as text, then a compiler turns it into a program the computer can run. On your own computer you would install a compiler such as GCC (g++), Clang or Visual C++. In this tutorial the online editor compiles and runs your code for you.

Your first program

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    std::cout << "Welcome to C++." << '\n';
    return 0;
}
Output
Hello, World!
Welcome to C++.
  • #include <iostream> brings in the input and output part of the standard library, which declares std::cout.
  • int main() is where the program starts. Every program has exactly one main.
  • std::cout is the console output. The output operator << sends the value on its right to it; several can follow each other.
  • std::endl ends the line (and flushes the output); the newline character '\n' also ends the line.
  • return 0; tells the system the program finished well. In main you may leave it out: C++ then returns 0 for you.
  • Every statement ends with a semicolon.

Compiling in the terminal

When you press Run, the editor types:

g++ -Wall main.cpp -o main && ./main

g++ compiles main.cpp into a program called main; -Wall asks for warnings about suspicious code. ./main runs it, but only if compiling worked (&&).

using namespace std

Everything in the standard library lives in the namespace std, which is why the names start with std::. Many beginner examples add using namespace std; so they can write cout and endl alone:

#include <iostream>
using namespace std;

int main() {
    cout << "Shorter to type" << endl;
    return 0;
}
Output
Shorter to type

It is fine in small programs like these. In larger programs and in header files, professionals write std:: explicitly, because a using line can cause name clashes. This tutorial uses both styles.

Practice exercises

  1. Print your name and your city on two lines.
  2. Print the same two lines with a single cout statement.
  3. Remove std:: before cout (without using namespace std;) and read the error and the hint.