Your First Program

Every C++ program starts in a function called main. This lesson walks through the classic first program line by line — includes, namespaces, output with std::cout — and shows what a compiler error looks like.

Hello, world

C++
#include <iostream>

int main() {
    std::cout << "Hello, world!\n";
    return 0;
}
Output
Hello, world!

Line by line

CodeMeaning
#include <iostream>Makes the input/output stream library available. Standard headers have no .h.
int main()The function where every program starts. It returns an int status to the operating system.
std::coutThe standard output stream (the terminal). std:: means it belongs to the standard library's namespace.
<<The insertion operator: sends the value on its right into the stream.
"Hello, world!\n"A string literal; \n is a newline.
return 0;Reports success. In main only, it's optional — reaching the end returns 0.

Chaining output

<< can be chained, and it works with numbers, characters and strings alike — no format specifiers needed:

C++
#include <iostream>

int main() {
    int year = 2026;
    double version = 20.0;
    char grade = 'A';
    std::cout << "Year: " << year << ", C++" << version << ", grade " << grade << "\n";
    std::cout << "2 + 3 = " << 2 + 3 << '\n';
}
Output
Year: 2026, C++20, grade A
2 + 3 = 5

\n or std::endl?

You'll often see std::endl used to end a line. It writes a newline and flushes the output buffer, which is slower when printing many lines. Prefer '\n' unless you specifically need the flush.

C++
#include <iostream>

int main() {
    std::cout << "with \\n\n";
    std::cout << "with std::endl" << std::endl;
}
Output
with \n
with std::endl

Namespaces and using

The standard library puts everything in the namespace std, so names don't clash with yours. Writing std:: every time is the recommended style. In small programs you'll also see using declarations:

C++
#include <iostream>

using std::cout;        // bring in just this name

int main() {
    cout << "Shorter to type\n";
}
Output
Shorter to type

Avoid using namespace std; in real code, and never in header files: it pulls hundreds of names into scope and can cause confusing clashes (for example with your own count or distance). Lesson 16 covers namespaces properly.

Comments and layout

C++
#include <iostream>

/* A block comment,
   which can span lines. */
int main() {
    int minutes = 135;                // a line comment
    std::cout << minutes / 60 << " h " << minutes % 60 << " min\n";
}
Output
2 h 15 min

Statements end with semicolons, braces group statements into blocks, and whitespace is free-form. C++ is case-sensitive: Main is not main.

A compile error

Leave out a semicolon and the compiler reports the file, line and column:

C++
#include <iostream>

int main() {
    std::cout << "Oops\n"
    return 0;
}
Compiler error
main.cpp:4:26: error: expected ';' after expression

Read the first error first — later ones are often caused by it. C++ compiler errors (especially with templates) can be long; the first line usually tells you what's wrong.

Try it yourself

Print a small receipt with the price total calculated by the program.

Try it yourself · main.cpp
Edit the code, copy it into main.cpp, then compile and run it with c++ -std=c++20 -Wall main.cpp -o main && ./main. How to set up a C++ compiler
Output
Coffee     3
Croissant  2
Total      5