In this C++ tutorial, you will learn how to write, understand, compile, and run a simple C++ Hello World program that prints Hello World! to standard output.
C++ Hello World Program
A Hello World program is a small C++ program used to verify that the basic source-code, compiler, and execution workflow is working. It also introduces the structure of a C++ program, including the main() function and console output with cout.
The following C++ program prints Hello World! to standard output.
main.cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
}
The file is named main.cpp. The .cpp extension is commonly used for C++ source files.
Output of the C++ Hello World Program
When the program runs successfully, it writes the following text to standard output:
Hello World!
How the C++ Hello World Program Works
Let us examine each part of the program.
Line 1: #include <iostream>
The #include <iostream> directive makes declarations from the standard input/output stream library available to the program. In this example, it is needed for cout, which writes text to standard output.
C++ provides many standard headers for different features. A program can include additional headers when it needs the declarations provided by those headers. Programs can also include project-specific header files.
Line 2: using namespace std;
The standard C++ library places names such as cout inside the std namespace. The statement using namespace std; allows this example to write cout instead of std::cout.
For larger programs, explicitly qualifying standard-library names with std:: is often preferred because it makes their namespace clear and helps avoid name conflicts.
Line 3: This is an empty line. Blank lines and ordinary whitespace can be used to make source code easier to read. They do not change the behavior of this program.
Line 4: int main() {
This defines the main function. In a normal hosted C++ program, execution starts in the main() function. The return type int means that main() returns an integer status value to the execution environment. The opening brace { begins the function body.
Line 5: cout << "Hello World!";
cout is the standard output stream. The stream insertion operator << sends the string literal "Hello World!" to that stream. The semicolon ; terminates the statement.
Line 6: }
The closing brace } ends the body of the main() function. Because execution reaches the end of main() without an explicit return statement, C++ treats it as a successful return from main(), equivalent to returning zero.
C++ Hello World Using std::cout
The same program can be written without using namespace std;. In that case, qualify cout with its namespace and write std::cout.
#include <iostream>
int main() {
std::cout << "Hello World!";
}
Both versions produce the same output. The difference is only in how the name cout is referenced.
Compile and Run the C++ Hello World Program
If a C++ compiler such as GCC is installed, save the source code as main.cpp and compile it from a terminal with g++.
g++ main.cpp -o hello
On Linux or macOS, run the generated executable with:
./hello
On Windows, when using a compiler that creates hello.exe, you can run the executable from the appropriate terminal with:
hello.exe
The program should print Hello World!. You can also run the same source file through an IDE, VS Code configured with a C++ compiler, or an online C++ compiler.
Print Hello World with a New Line in C++
The original example prints the text without explicitly adding a line break. To move the output position to the next line, append \n to the string:
#include <iostream>
int main() {
std::cout << "Hello World!\n";
}
You can also use std::endl:
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
}
Both forms place subsequent output on a new line. std::endl additionally flushes the output stream, so a newline character such as \n is commonly sufficient when an immediate flush is not required.
Common C++ Hello World Compilation Mistakes
- Missing
#include <iostream>:std::coutis declared by the<iostream>header. - Using
coutwithout its namespace: usestd::cout, or keep the existingusing namespace std;statement. - Missing semicolon: the output statement must end with
;. - Incorrect quotation marks: use normal double quotes around the string, such as
"Hello World!". - Saving the source with the wrong extension: save C++ source code with a suitable extension such as
.cpp. - No compiler configured: an editor such as VS Code edits the file, but a C++ compiler is still required to turn the source code into an executable program.
C++ Hello World Program Structure at a Glance
#include <iostream>provides the standard stream declarations used for console output.main()is where execution begins for this program.coutorstd::coutwrites data to standard output.<<inserts the text into the output stream."Hello World!"is a string literal.- The semicolon ends the output statement, and the braces delimit the body of
main().
TutorialKart.com