C++ "Hello, World!" Program
In this example, we will learn to create our First program named "Hello World" in C++ programming.
"Hello World!" Program
The "Hello, World!" program is the first step towards learning any programming language. It is one of the simple program you will learn that program gives the outputs Hello, World! on the screen. Since it's a very simple program, it's often used to introduce a new programming language to a beginner.
Let's see how C++ "Hello, World!" program works.
If you haven't already set up the environment to run C++ on your computer, visit Install C++ on Your Computer.
Program to Display "Hello, World!"
// Your First C++ Program to print "Hello World!"
// header file for input-output functions
#include <iostream>
// main function - where the execution of program begains
int main()
{
// print hello world
std::cout << "Hello World!";
return 0;
}
Output
Hello World!
How "Hello, World!" program works?
// Your First C++ Program to print "Hello World!"
:- In C++, any line starting with//
is a comment. Comments are intended for the person reading the code to better understand the functionality of the program. It is completely ignored by the C++ compiler.#include <iostream>
:- The#include
is a preprocessor directive used to include files in our program. The above code is including the contents of the iostream file which contains declarations of all the standard input/output liberary functions.
This allows us to usecout
in our program to print output on the screen.-
For now, just remember that we need to use
#include <iostream>
to usecout
that allows us to print output on the screen. int main() {...}
:- A valid C++ program must have themain()
function. A function is a group of statements that are designed to perform a specific task. The curly braces indicate the start and the end of the function.
The execution of code beings from this function.std::cout << "Hello World!";
:-std::cout
prints the content inside the quotation marks""
. It must be followed by<<
followed by the format string. In our example,"Hello World!"
is the format string.
Note:
;
is used to indicate the end of a statement.return 0;
:- Thereturn 0;
statement is the "Exit status" of the program. In simple terms, the program ends with this statement.
Things to remember from the above program
- We must include iostream if we want to use
std::cout
in our C++ programs. - We use
std:cout
in order to print output on the screen. - The execution of code begins with the
main()
function. This function is mandatory. This is a valid C++ program that does nothing. - It's mandatory To prints the Statement inside the quotation marks
""
. - Semi-colon
;
is used to indicate at the end of a statement. - The
return 0;
statement is used to "End" the program.
int main() {
// you can Write your code here
// This is a valid C++ program that does nothing.
}
Next Example
We hope that this Example helped you develop better understanding of the concept of "Hello world!" Program in C++.
Keep Learning : )
In the next Example, we will learn about C++ Program to Print Number Entered By User
.