Hello world program in C++

A popular and relatively simple Object Oriented Programming language is C++. You may learn C++ programming by doing the following:

Utilizing a text editor to create your program and storing it with the appropriate extension (.CPP, .C, .CP)
using an online IDE or a compiler to compile your program

recognizing the fundamental terms.
The "Hello World" program is one of the simplest programs you will learn and the first step in learning any programming language. All you have to do is have the screen say, "Hello World." Let’s examine the program now:

C++ program to display "Hello World"

// Header file for input-output functions
#include <iostream>
using namespace std;  
// Main() function: where the execution of the program begins
int main()
{
    // prints hello world
    cout<<"Hello World!";  
    return 0;
}
Output: Hello world!

Understand the terminologies of the above program:

  1. C++ program to display “Hello World”, This line serves as a comment. To display further program-related information, a comment is utilized. There is no programming logic in a comment. A compiler simply passes over a line of code when it comes across a comment. In C++, a comment is defined as any line beginning with ‘//’ without quotes OR between //.
  2. All lines in C++ that begin with the pound (#) symbol are known as directives, and they are handled by a preprocessor, a program that the compiler calls on. The compiler is instructed to include a file and #include< iostream > via the #include directive. The standard iostream file, which includes declarations for all of the standard input/output library functions, is included, instructing the compiler to do so.
  3. Using the namespace std, the application can import the entire std namespace into its current namespace. It’s generally agreed upon that utilizing the namespace std in a statement is not a good idea. All type definitions from a namespace are effectively added to the current scope when we import it. The standard namespace is enormous. The alternative to this statement is to use the scope operator(::) each time we create a type to identify the namespace to which the identifier belongs.
  4. This line declares the function "main" which returns data of the integer type. int main() A function is a collection of statements created with a specific purpose in mind. No matter where a function appears in a C++ program, it is the main() function that starts the program’s execution. Thus, the main() method is a need for all C++ programs.
  5. Both the opening braces ‘{‘ and the closing braces ‘}’ denote the start and finish of the main function, respectively. The area in between these two is the main function’s body.
  6. std::cout<<“Hello World”;: The compiler is instructed to show the message "Hello World" on the screen by this line. In C++, this line is referred to as a statement. Every sentence has a certain purpose. A statement is concluded with a semicolon, ";". The statement is marked with a semicolon at the end to signify that it is finished. The standard character output device, which is often the desktop screen, is identified with the std::cout command. The character "<<" is followed by everything that is displayed on the output device.
  7. Return 0; is a statement as well. This sentence denotes the completion of a function and is used to return a value from one. Basically, this statement is used in functions to return the outcomes of the actions taken by a function.
  8. As you can see, the return statement and bracket have been indented or shifted to the right. To make the code easier to read, this is done. It doesn’t matter much in a program like Hello World, but as the program gets more complicated, it makes the code more understandable and less prone to mistakes. Therefore, to make the code more legible, you must always utilize indentations and comment

Important Things to Keep in Mind When Writing C++ Code

  • Include all relevant header files to ensure that functions run without interruption. For instance, in order to utilize std::cin and std::cout, < iostream> must be included.
  • The main() function is where code execution starts.
  • The use of indentation and comments in programs is recommended for simple comprehension.
  • Statements are printed using cout, and inputs are received using cin.

Other C++ Programs

C++ Program to Add Two Numbers
C++ Program to Reverse a Number
C++ program for heap sort
Bubble Sort in C++ with Program Code and Example

Leave a Reply

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