This Sample Program of C++ print Hello World!
#include<iostream>
using namespace std;
int main() {
cout<<“Hello World!”;
return 0;
}
Brief explanation of each line of the program is given below:
#include<iostream>
This line includes the iostream header file, which provides functionalities for input and output operations, such as cout (for printing output) and cin (for reading input).
Here #include is preprocessor directive. Preprocessor directives are commands used to include files in our program. They are beginning with a # symbol. The above code is including the contents of the iostream file. This allows us to use cout in our program to print output on the screen.
<iostream> is a header file. It provides declarations and definitions for standard input/output functionalities. iostream stands for “Input/Output Stream“. It must be included at the beginning of a C++ program using #include preprocessor directive.
using namespace std;
This directive makes all names from the std (standard) namespace directly accessible without needing to prefix them with std::. For example, instead of writing std::cout, std::cin, std::endl etc. one can simply write cout, cin and endl.
std is a namespace. Itserves as a container for all the entities (classes, functions, objects, etc.) that are part of the C++ standard library. The objects and functions declared in iostream (like cout and cin) are members of the std namespace. Therefore, to use them without explicitly qualifying them with std:: , we would typically employusing namespace std; in our code.
Namespaces are a mechanism in C++ to organize code into logical groups and prevent name collisions. In large projects, especially those incorporating multiple libraries, different libraries might use the same names for functions, classes, or variables. Namespaces help distinguish these identifiers by providing a unique scope.
int main() { ….. }
This is the main function where program execution begins.
Here main() is the function name and int is the return type of this function. It is a compulsory part of every C++ program. A valid C++ program must have the main() function. It is a special function used to inform the compiler that the program starts from there. Execution of every C++ program starts from main() function. int indicates that the main function is expected to return an integer value.
The curly braces {} define the scope of the main function, containing the instructions to be executed. The curly braces indicate the start and the end of the function. The opening curly bracket ‘{’ indicates the beginning of the main function and the closing curly bracket ‘}’ indicates the end of the main function.
cout<<“Hello World!”;
This line uses cout to print the string “Hello, World!”. The string to be printed is enclosed in double quotes.
The semicolon serves as the statement terminator. Semicolon character at the end of the statement is used to indicate that the statement is ending there. Each executable statement should have a ‘;’ to inform the compiler that the statement has ended. Thus, in ‘C’ the semicolon terminates each statement.
return 0;
The return 0; statement is the “Exit status” of the program. This statement concludes the main function. The return statement is used to return a value from a function and indicates the finishing of a function. The value ‘0’ means successful execution of this function.