This is a simple “Hello, World!” C++ program to display “Hello World” text on the computer screen or display device.
Typically, the best way to learn programming is by writing code. The first program beginners write is “Hello, World!” which is often used to illustrate the syntax of a programming language. You can take a look at the list of Hello World Programs in 300 Programming Languages to see how “Hello, World!” looks like in different programming languages.
Let’s take a look at how “Hello World” program looks in C++.
Example 1: C++ Hello World Program
1 2 3 4 5 6 7 8 9 10 11 | //My first C++ Program #include <iostream> using namespace std; int main() { std::cout << "Hello, World!"; return 0; } |
Output of C++ Program
1 | Hello, World! |
The first line of this program is a comment which is indicated by double forward slashes i.e. //
Then lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor in C++.
Every C++ program run from the main() function regardless of where the function is actually located within the code.
The cout is the standard output stream which prints the “Hello, World!” text on the computer screen.
Finally, the return 0; is the exit status of the program which causes the C++ program to terminate.
Example 2: C++ Hello World Program
1 2 3 4 5 6 7 8 | #include <iostream> using namespace std; int main() { cout << "Hello, World!"; return 0; } |
Output of C++ Program
1 | Hello, World! |