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
#include <iostream> int main() { std::cout << "Hello, world!\n"; return 0; }
Hello, world!
Line by line
| Code | Meaning |
|---|---|
#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::cout | The 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:
#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'; }
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.
#include <iostream> int main() { std::cout << "with \\n\n"; std::cout << "with std::endl" << std::endl; }
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:
#include <iostream> using std::cout; // bring in just this name int main() { cout << "Shorter to type\n"; }
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
#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"; }
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:
#include <iostream> int main() { std::cout << "Oops\n" return 0; }
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.
Coffee 3 Croissant 2 Total 5