The Program
Here is the complete Hello World program in C++. Press Run to compile and execute it in the browser:
It prints one line:
Hello, World!
Five lines of code, and each one does a specific job. The sections below go through them in order, then cover how to build the same file on your own computer and what the first compiler errors usually mean.
Line by Line
#include <iostream>
Lines that start with # are instructions to the preprocessor, which runs before the compiler proper. #include <iostream> pastes in the declarations from the standard header <iostream> (input/output streams). That header is where std::cout and std::endl are declared. C++ has no built-in print statement; printing is part of the standard library, and you opt into it with this line.
int main() {
Every C++ program starts running at a function called main. There must be exactly one, and it must be spelled in lowercase. The int before it says that main gives back an integer to the operating system when it finishes. The empty parentheses mean it takes no arguments here, and the opening brace { starts the function body.
std::cout << "Hello, World!" << std::endl;
This line does the actual printing.
std::coutis the standard output stream, usually the terminal. Thestd::prefix says thatcoutlives in thestdnamespace, where the whole standard library is kept.<<is the insertion operator. It sends the value on its right into the stream on its left. You can chain it, and each<<adds the next piece to the same output."Hello, World!"is a string literal: text in double quotes.std::endlends the line.- The semicolon ends the statement. Every statement in C++ needs one.
return 0;
return ends main and hands a value back to the operating system. By convention 0 means the program succeeded, and any other number signals an error. Scripts and build tools read this exit code. Inside main the line is optional: if execution reaches the closing brace, C++ behaves as if you wrote return 0;.
The Closing Brace
The closing brace ends the body of main, and with it the program.
std::endl vs '\n'
There are two common ways to end a line, and both appear in real code:
Output:
Hello, World!
Hello again!
Line one
Line two
The visible result is the same. The difference is that std::endl writes a newline and then flushes the stream, which pushes any buffered text to the terminal immediately. '\n' writes only the newline, and the stream flushes on its own schedule (at the latest when the program ends). In a loop that prints thousands of lines, the extra flushes from std::endl make a measurable difference, so many C++ programmers default to '\n' and use std::endl only when the text has to appear right away. The \n escape also works inside a string, as the third line shows. The input and output page covers cout and cin in more depth.
The using namespace std; Version
Many tutorials write Hello World like this:
using namespace std; tells the compiler to look in the std namespace for names it cannot otherwise find, so cout and endl work without the prefix. It is fine for small programs and exercises. In larger code and in header files it is usually avoided, because it pulls hundreds of standard names into scope and they can clash with your own (a function of yours called count or distance, for example). Writing std::cout costs five characters and removes the risk.
Compiling and Running Hello World on Your Computer
The browser editor compiles for you. On your own machine you do the same two steps yourself: compile the source file into an executable, then run the executable. You need a C++ compiler installed first; the install guide covers setting one up on Windows, macOS and Linux.
Save the program as hello.cpp, open a terminal in the same folder, and compile it:
g++ hello.cpp -o hello
If nothing is printed, it worked, and there is now an executable called hello (hello.exe on Windows) next to the source file. Run it:
./hello
On Windows, run hello.exe or just hello instead. On macOS, clang++ hello.cpp -o hello works the same way, and g++ there is usually Clang under another name.
Two flags are worth adding from the start: -std=c++17 (or -std=c++20) picks the language standard, and -Wall -Wextra turns on warnings that catch real mistakes:
g++ -std=c++17 -Wall -Wextra hello.cpp -o hello
The compiling C++ page explains what these flags do, what -o means, and why you get a file called a.out without it.
Common First Errors
The error text below is from GCC (g++). Clang words its messages differently but points at the same line.
Missing semicolon.
hello.cpp:5:5: error: expected ';' before 'return'
The compiler notices the missing ; only when it reaches the next token, so the line number usually points one line after the mistake. Check the end of the previous line.
Missing #include <iostream>.
error: 'cout' is not a member of 'std'
std::cout is declared in <iostream>. Add the include at the top of the file.
Missing std::.
error: 'cout' was not declared in this scope; did you mean 'std::cout'?
Either write std::cout and std::endl, or add using namespace std; after the include.
Main instead of main. C++ is case sensitive. A program with int Main() compiles, then fails at the link step with undefined reference to 'main', because the linker cannot find the function the program is supposed to start from.
Compiling with gcc instead of g++. gcc hello.cpp compiles the C++ code but does not link the C++ standard library, so you get a long list of undefined reference to std::... errors. Use g++ (or clang++) for C++ files.
Curly quotes. Code copied from a word processor or a web page can contain typographic quotes (“Hello”) instead of straight ones ("Hello"). GCC reports stray '\342' in program. Retype the quotes in your editor.
Old headers and void main. Some very old tutorials use #include <iostream.h> or void main(). Neither is standard C++: the header is <iostream> with no .h, and main must return int. Modern compilers reject both.
Printing With std::println in C++23
C++23 added a shorter way to print a line, std::println from the <print> header:
#include <print>
int main() {
std::println("Hello, World!");
}
It adds the newline for you and supports {} placeholders for values. Support arrived in GCC 14, Clang 18 with libc++, and recent versions of Visual Studio, and it needs -std=c++23. Most existing code and tutorials still use std::cout, so it is worth being fluent in both.
Frequently Asked Questions
How do you write Hello World in C++?
Start with #include <iostream>, then define int main() and put std::cout << "Hello, World!" << std::endl; inside it, followed by return 0;. Save the file as hello.cpp, compile it with g++ hello.cpp -o hello, and run ./hello.
Why does C++ Hello World need #include <iostream>?
std::cout and std::endl are declared in the <iostream> header, not built into the language. Without the include the compiler does not know those names and stops with an error such as 'cout' is not a member of 'std'.
What is the difference between std::endl and '\n'?
Both end the line. std::endl also flushes the output buffer, forcing the text out immediately. '\n' only writes the newline and lets the stream flush when it is ready, which is faster when you print many lines. For a single Hello World line there is no visible difference.
Do I need return 0 at the end of main in C++?
No. main is the one function where C++ lets you leave out the return; reaching the closing brace is treated as return 0;, which tells the operating system the program succeeded. Many people write it anyway to make the exit status explicit.