C++ course menu
Your First C++ Program: Hello World Explained Line by Line
Write, compile and run your first C++ program. Understand #include, int main(), std::cout, the << operator, \n vs std::endl, return 0, and how to fix the most common first-program errors.
On this page
Every programmer's first program prints a message. In C++ that is five short lines, and each one teaches you something important.
The program#
Save this as hello.cpp:
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
return 0;
}Compile it and run it:
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello
./helloHello, World!Line by line#
Line 1: #include <iostream> brings in the standard library's input/output tools. Lines starting with # are instructions to the preprocessor, which runs before the compiler. iostream stands for "input/output stream", and it gives us std::cout.
Line 3: int main() defines the function where every C++ program starts. int means the function returns a whole number to the operating system when it finishes. The curly braces { and } mark the start and end of the function's body.
Line 4: std::cout << "Hello, World!\n";
std::cout("character output") is the standard output, normally your terminal.<<sends the value on its right into the stream on its left."Hello, World!\n"is a string literal.\nis the newline character, which moves to the next line.- The semicolon
;ends the statement. Forgetting it is the most common beginner error. std::means "from the standard library's namespace". You will learn more in Syntax.
Line 5: return 0; ends main and tells the operating system the program succeeded. A non-zero value means an error. In main only, you may leave this line out and C++ returns 0 automatically.
Printing several things#
You can chain << to print several values in one statement, including numbers:
#include <iostream>
int main() {
std::cout << "C++ was created in " << 1979 << ".\n";
std::cout << "2 + 3 = " << 2 + 3 << '\n';
std::cout << "One line, ";
std::cout << "same line.\n";
}C++ was created in 1979.
2 + 3 = 5
One line, same line.Notice that std::cout does not add a newline by itself. Text keeps going on the same line until you print \n. A single character such as '\n' can be written in single quotes.
\n or std::endl?#
You will often see std::endl in other tutorials:
#include <iostream>
int main() {
std::cout << "Line one" << std::endl;
std::cout << "Line two\n";
}Line one
Line twoBoth end the line. std::endl also flushes the output (forces it to be written immediately), which is slower when printing many lines. Prefer '\n', and use std::endl only when you need the output to appear right away.
Escape sequences#
A backslash inside a string starts an escape sequence:
#include <iostream>
int main() {
std::cout << "Name:\tAsha\n";
std::cout << "She said \"hi\".\n";
std::cout << "Path: C:\\Users\\asha\n";
}Name: Asha
She said "hi".
Path: C:\Users\ashaReading compiler errors#
If you make a mistake, the compiler stops and tells you where. For example, without the semicolon:
#include <iostream>
int main() {
std::cout << "Hello, World!\n"
}g++ reports something like:
hello.cpp: In function 'int main()':
hello.cpp:4:35: error: expected ';' before '}' tokenThe message gives the file, line and column (4:35), and what it expected. Always fix the first error first; later errors are often caused by it.
Common mistakes#
Practice#
Practice exercise
Write a program that prints this short profile, using one std::cout statement per line and a tab between each label and value:
Name: Asha
City: Pune
Year: 2026Show hint
Use \t for the tab and \n at the end of each line. The year can be printed as a number.
Show solution
#include <iostream>
int main() {
std::cout << "Name:\tAsha\n";
std::cout << "City:\tPune\n";
std::cout << "Year:\t" << 2026 << '\n';
}Name: Asha
City: Pune
Year: 2026Key takeaway#
Finished this lesson?