Print "Hello World" - Our First C++ Program

In this tutorial, we will work on our first C++ program and have a detailed explanation of each part of it.

Print "Hello World" - Our First C++ Program

Now its time to have a look at our first C++ program. We have already talked about some of the basic concepts of C++ and now we will work on actual programs and along with theory related to the program and topics left to cover.

FIRST C++ PROGRAM:

Now we will take up a simple example of a C++ program that prints a string on the screen. We will print Hello World as a string in this print. So let's get started with a program to print Hello World in C++.

Program to print a string on the screen.

//MY FIRST C++ PROGRAM
#include
int main()
{
    cout<<"Hello World";
    return 0;
}

The above program is one of the simplest C++ programs, It includes basic elements that every C++ program has. Let's break down each part of this program and learn something about it.

// MY FIRST C++ PROGRAM:

This is a comment. A line beginning with // are comments in C++. The compiler does not execute comments. Comments are included only for the description of our code and to make our code easy to understand.

#include

The statements that begin with # (hash/pound) sign are directives for the preprocessor which means that these statements are processed before the compilation of the program takes place. The #include statement tells the compiler's preprocessor to include the header file iostream in the program,  iostream is mostly used for cin and cout which are input and output operations in C++.

Note: In the newer versions of compilers we use #include in place of #include you can notice that difference between both methods is that one uses ".h" and while the other one doesn't use it, Some header files still use the ".h" extension in new compilers. We will look more about header files and their use in other tutorials.

int main()

This line the starting of the main function of the program. The main() function is the point at which the C++ program starts its execution. Every C++ program must have function named main(). The int main() means this function will return the integer value after its execution. So the basic syntax to define a function in C++ is 

data_type fucntion_name ()
{
 Statement 1;
 Statement 2;
}

Here, data_type is the return type of the function, function_name is the name of the function it can be anything but there are some rules which need s to followed that we have already discussed in the previous tutorial. () inside these brackets, we pass the value to the function we will look more about it in other tutorials. { } these curly braces mark the start and end of our function the statement inside it will only be executed when we call our function.

cout<<"Hello World";

This statement is the most important in this program because it does the main task for which we are writing this program. The cout is the standard output stream in C++ and the above statement inserts a sequence of characters - "Hello World" here - into the output stream. cout is declared in the header file iostream.h so in order to use cout int our program we have included "iostream.h" header file. Every statement in the C++ program ends with a semicolon ";". This is used to mark the end of our statement if any statement ended then our compiler will give us an error while compiling the program. So make sure to use a semicolon after every executable statement in the C++ program and keep in mind that C++ is a case sensitive language so "cout" and "Cout" have a different meaning in the program which means we have to be always aware of the case of the statement.

return 0;

The return instruction makes the main() function to finish and it returns a value. Returning zero is the most common way of telling that the program has terminated normally.

We will look more about it in future tutorials.

Congratulation on writing the first C++ program we wish you the best of luck for your journey to learn C++ with us. We will be covering more topics in other tutorials so for now, this is the end of this tutorial we will learn more in the next C++ tutorial. 

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0