Learning C++ programming can be divided into the following steps:
- In a text editor, write your code and save it with the appropriate extension (.CPP, .C, .CP).
- Use a compiler or an online IDE to compile your program.
- Understanding the fundamental terminologies.
The Hello World program is the first step in learning any programming language. Also, it’s very easy to write such a simple program. You need to show the message Hello World!
on the screen. Now, let’s look at the program that will print the word ‘Hello World!’ message on the screen.
#include <iostream> // Include the input/output stream header using namespace std; int main() { // Main function cout << "Hello World!" << endl; // Output message return 0; }
OUTPUT OF THE PROGRAM
Hello World!
Let’s go through each line and the terms used in the above program:
#include
All lines in C++ that begin with the pound #
symbol are referred to as directives, and they are handled by the preprocessor, which is a program run by the compiler. The #include
directive instructs the compiler to include a file, while the #include <iostream>
directive instructs the compiler to include an iostream
. It informs the compiler to include the standard iostream
file, containing declarations for all of the library’s standard input/output functions.
using namespace std;
This is used to import the whole std namespace into the program’s current namespace. The use of the namespace std in a statement is typically looked upon. Importing a namespace effectively pulls all type definitions into the current scope. The standard namespace is enormous. An option to this statement is to use the scope operator ::
to indicate the namespace to which the identifier belongs each time we create a type.
int main()
This line declares the main()
function, which returns data of the integer type. A function is a collection of statements that accomplish a specific function. Regardless of where the main()
function is situated in the program, the execution of every C++ program starts with it. As a result, a main()
function is required in every C++ program.
Body Of The Program (i.e., Main() function) '{ }':
The beginning of the main function is indicated by the opening braces {
and the ending of the main function is indicated by the closing braces }
. Everything between these two curly braces { }
comprises the body of the main ()
function.
cout<< "Hello World!";
This line commands the compiler to print ‘Hello World!’ on the screen. In C++, this line is known as a statement. Every statement is intended to accomplish a specific task. To finish a statement, the semicolon ;
is used. The semicolon character at the end of the sentence shows that the statement is coming to an end. The standard character output device, usually the desktop screen, is identified via the cout
command. Everything that comes after the character <<
is sent to the output device.
return 0;
This is also a declaration or statement. This statement is used to signify the end of a function and to return
a value from it. This statement is used in functions to return the results of the actions that the function has performed.
// Output message
This line serves as a comment. A comment is used to display additional program information. There is no programming logic in a comment. When a compiler encounters a comment, it simply passes over that line of code. In C++, every line starting with //
without quotes OR between /*...*/
is considered a comment by the compiler.