A Simple Hello World C++ Program to understand basics of C++ Programming

by - February 15, 2019

Hello World! Program in Cpp with explanation

Here is a Simple C++ Program with Explanation which Prints Hello World! Welcome to C++ on the  Screen.


1.       // A Simple C++ Program
2.       #include <iostream>
3.       #include <conio.h>
4.       using namespace std;
5.       // Function main begins program execution
6.       int main()
7.       {
8.             cout << "Hello World!";
9.             cout << "\nWelcome to C++!";
10.         return 0;
11.         _getch();

12.   }


Output
Hello World!
Welcome to C++!


Comment: (Line 1 & 5)

// A Simple C++ Program
The line beginning with a double backslash (//) is a comment. Commenting on your program makes it readable for a large number of people. Consider you are making a program, and later you send it to your friend. Your friend might feel difficult to understand your code. Here commenting will benefit both of you.

Pre-Processor Directive: (Line 2 & 3)

#include <iostream> & #include <conio.h>
Before the actual compilation, the preprocessor is a program invoked by the compiler that modifies the source code. This type of modification is done according to the preprocessor directives that we include in our source files. The directives all start with a hash sign (#). So they are easily distinguished from normal programming code. The first directive we’ll look at is #include, which inserts the contents of a specific file into the current one. If the filename is enclosed between angular brackets <>, the compiler will search for the file in the default directory where it is configured to look for the standard header files. If we instead specify the filename between double-quotes, the compiler first searches for the file in the same directory as the source file, and in case it is not there it will then search among the standard header files.

using namespace std: (Line 4)

using namespace std;
Std is the standard namespace. Cout, cin and many other things are defined in it. (This means that one way to call them is by using std::cout and std::cin.) The keyword using technical means, use this whenever you can. It refers, in this case, to the std namespace.

Function Definition: (Line 6 – 12)

This portion consists of two parts; Function Header and Function Definition (Line 7-12).

Function Header (int main())

Function Header contains return Data-type (int), Function Name (main) and input(s) in parentheses. Return data-type should be according to what value the function will return. It may be int, float or a character. A function name is an identifier, and it should be named carefully.

Read this article to name an identifier.

Its syntax would be
return data-type functionName(inputs)

Function Body (Line 7-12)

All the code enclosed in braces {} is called function body. Function body defines the work of the function.

cout: (Line 8 & 9)

Cout is an object lying in standard namespace. It is used for printing something on the console window.

return statement: (Line 10)

Return statement used to return something from the function. Here Return 0 means successful execution and return 1 indicate unsuccessful execution.

getch(): (Line 11)

_getch() or getch() is a function lying in conio.h header file. It is mostly used in the Visual Studio. Its function is that: It waits for the user to press any key for the output screen to disappear.

About Main Function:

The main function is a user-defined function, as it depends upon the user what he has written inside it. Execution of any C++ programs always starts from the main function. It is the main point of any C++ Program. Without it, no program will run, since execution begins from the main function and also ends on it. There must be the main function in every program.

Thanks for reading. If you like this article then give a Thumbs up. Don't Forget to write your opinion in the comment box and if you think that this article will help any of your friends then do share this article.

Note: C++ is a case sensitive programming language. Make sure you use correct spelling and capitalization. Here in this article, there may be mistakes because it follows basic English rule to be written. So Kindly follow the method told in the program.

You May Also Like

1 comments