Hello World Program in C++
The steps for “Hello World” C++ program are as follows:
- Write a C++ code given below in an editor.
- Save the file with .cpp
- Compile the code using C++ compiler or using online IDE.
- Check the output of the code.
“Hello World” program is considered as most basic and first program while learning any technology or language. In this program, user only prints “Hello World” statement to the screen.
Code:
// "Hello World" Program in C++ #include<iostream> using namespace std; int main() { cout<<"Hello World"; return 0; }
Output:
Hello World
Let us understand the above program line by line
- // "Hello World" Program in C++
-
- In C++, any statement starts with ‘//’ is called a comment line.
- Comment line is not taken into consideration by compiler i.e. compiler don’t consider this line while compilation.
- Comment is generally used to specify author name or to specify the meaning/logic of any statement.
- In C++, comments are given to single line statement and multiline statements.
-
-
- Single line comments // statements
-
-
-
- Multiline comments /* statements */
-
- #include<iostream>
- The statement starts with (#) are called as directives in C++.
- Here, #include tells compiler to add include the header file which is “iostream.”
- The iostream header file provides inbuilt input/output objects and functions. We can use the “cout” object only by including “iostream” header file.
- using namespace std;
- This statement add the “std” namespace into the current file.
- Add an entire “std” package sometimes consider as bad practice because it imports all declarations from namespace “std” that contains the declaration for the cout and cin objects. Hence “std::cout” is consider as a good practice to use in program instead of importing entire “std” namespace into the code.
- int main()
- In “int main ()” defines the function definition where main () is the function and int is the return type of function.
- The main () is consider as the starting point of the program and compiler starts the execution from the main () function only. And hence it is important to write main () function in every C++ program.
- As the return type of main () function is int, function will return an integer value from the function and the value that this main () functions return we see that in further article.
- { and }
- The statements written inside these curly brackets { } are called the function body.
- Here, “{” represents the starting of the function and “}” represents the closing of the function.
- cout<<"Hello World";
- This statement tell compiler to print "Hello World" to output screen.
- Here, cout is the object of ostream class which we included I current program using “iostream” header file.
- The cout object is used to print the statement on the screen and it must use with << operator.
- The cout print the statement to output screen using << operator and it print the string written inside the " " double quotes.
- The above statement cout<<"Hello World"; is terminated with semicolon which represents the end of the statement. In C++, every statement must be terminated with semicolon (;).
- return 0;
- This is also a statement in C++ and hence it is also terminated with semicolon (;).
- To return a value from the function, return statement is used in C++.
- The “return 0” statement is returning value 0 which is of type int and this int type is similar to the function return type.