Hello World
The classic first program that prints "Hello World" to the console
C++ Hello World Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
// Necessary header files for input output functions
#include <iostream>
using namespace std;
// main() function: where the execution of
// C++ program begins
int main() {
// This statement prints "Hello World"
cout << "Hello World";
return 0;
}Hello World
Understanding Hello World
The "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. It is the basic program that demonstrates the working of the coding process. All you have to do is display the message "Hello World" on the output screen.
Preprocessor Directives
The #include is a preprocessor directive that tells the compiler to include the content of a file in the source code. For example, #include<iostream> tells the compiler to include the input-output library which contains all C++'s input/output library functions.
Header Files
A header file contains functions, global variables and macros, etc that can be used by any other C program by including that particular header file using "#include" preprocessor. Header files generally ends with the .h extension although in modern C++ many header files do not have the .h extension.
// header file with .h extension
#include<string.h>
// header file without .h extension
#include <iostream> Namespace
Main Function
The main() function is the entry point of every C++ program when the program is executed the code written in the main functions is executed first. The opening braces '{' indicates the beginning of the main function and the closing braces '}' indicates the ending of the main function.
Comments
The comment are used to display additional information about the program that do not contain any programming logic and are not the part of executable program and are purely used for documentation and provide explanation or notes about the code. When a comment is encountered by a compiler, the compiler simply skips that line of code.
Output Statement
The cout is a output statement in C++ that is used to display output on the console screen. Everything followed by the character << in double quotes " " is displayed on the output screen. The semi-colon character at the end of the statement is used to indicate that the statement is ending there.
Return Statement
The return statement is used to return a value from a function and indicates the finishing of a function. Here, it is used to sent the signal of successful execution of the main function.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.