C++

Hello World – First C++ Program

We will write and comprehend the first C++ program in this tutorial. We are creating a straightforward C++ application that produces the message “Hello World!” Watch the program first, and then we’ll go through every single element in depth.

Hello World Program in C++

/*
* Multiple line
* comment
*/
#include<iostream>

//Single line comment
using namespace std;

//This is where the execution of program begins
int main()
{
   // displays Hello World! on screen
   cout<<"Hello World!";

   return 0;
}

Output:

Hello World!

Let’s talk about every single element of the aforementioned software.

1. Comments – The program above has two different types of comments.

// This is a single line comment
/* This is a multiple line comment
* suitable for long comments
*/

As the name implies, comments are just text that programmers write while creating new code. Although comments have no bearing on the logic of your program, they should be tied to the code and have some significance so that when someone else looks at your code, they can comprehend what you did simply by reading your comment.

For example:

/* This function adds two integer numbers 
 * and returns the result as an integer value
 */
int sum(int num1, int num2) {
   return num1+num2;
}

Now, just by reading my reply, a reader will be able to grasp what I accomplished there. This makes your code more readable, which is a crucial factor when working on a project with your teammates.

2. #include<iostream> – The compiler is instructed to include the iostream file by this statement. This file provides input/output functions that have already been defined, which we can use in our software.

3. using namespace std; –  A namespace is similar to a region where functions, variables, etc. are present and have a scope specific to that region. In this case, the namespace name std instructs the compiler to search in that specific area for all the variables, functions, etc. I won’t go into depth about this because it might make you confused. This subject has been covered in a different tutorial with examples. Simply adhere to the tutorial’s suggested order, and everything will work out.

4. int main() – As implied by the function’s name, this is the main function of our program, and it is this function that starts the program’s execution. The int in this case serves as a return type, telling the compiler that this function will return an integer value. The return 0 statement at the end of the main function is there primarily for that reason.

5. cout << “Hello World!”; – The cout object is a component of the iostream file, and its function is to display the text enclosed in double quotes exactly as it appears on the screen. The value of variables can also be displayed on screen using this object (don’t worry, we’ll see how to achieve so in the upcoming tutorials).

6. return 0; – This statement causes the main() function to return a result of 0, indicating that the function’s execution was successful. Execution failure is represented by the number 1.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button