Now, we have understood all the basic concepts to create our first C++ program, so let’s take a look how to print a simple ‘Hello World’, statement in C++.

#include <iostream.h>
// main() is where program execution begins.
void main() 
   cout << "Hello World"; // prints Hello World

The above lines of code will print ‘Hello World’.

Now, you know what to do, so let us dig deeper and find out the meaning of some parts of the program:

1. The very first line is:

#include <iostream.h>

Here, iostream.h is a header file that contains necessary information, which will be utilized in a C++ program. It contains definition of functions, classes, etc. so one can use them easily.

2. The next line

// main() is where program execution begins.

Begins with a double slash, now this is a C++ comment, which is for better understanding of the written code. We will learn about comments later on.

3. The next line is:

void main()

Here, void is a return type and it signifies that the function does not return anything, whereas main is the name of a function. All the C++ programs begin execution at the main() function. It is like the starting point of a C++ program. The parenthesis after the word main identify that it is a function.

4. The next line has an open curly braces, signifying that the body of main() has started.

5. The next line is

cout << "Hello World";

It causes the message  “Hello World” to be displayed on the screen, here, cout is used to print the message written within the double quotes. Now, cout performs a predefined function, this function is mentioned in the iostream header file.
Along with this is a comment, which describes what this statement will actually do.

6. The last line of the code is a closing curly brace thus, showing that the body of the
main() has ended.

Report Error/ Suggestion

Related Posts:


Interview Questions

CopyRight © 2020

CopyRight © 2020