Home » C++ » Structure of a Simple C++ Program: ‘Hello World’ Example | Class: 002 | C++ Complete Course

Structure of a Simple C++ Program: ‘Hello World’ Example | Class: 002 | C++ Complete Course2 min read

Writing a Simple C Plus Plus Program: Hello World Example

by TechABU
220 Views
How To Print Hello World In C++

Learning C++ programming can be divided into the following steps:

  • In a text editor, write your code and save it with the appropriate extension (.CPP, .C, .CP).
  • Using a compiler or an online IDE to compile your program.
  • Understanding the fundamental terminologies.

The “Hello World” program is the first step in learning any programming language. Also, it’s very easy to write such a simple program. You need to show the message “Hello World” on the screen. Now, let’s look at the program that will print the word ‘Hello World!’ message on the screen.

//Cpp to print the message 'Hello World' on the screen

#include <iostream>	//header files
using namespace std;

int main()	//main function

{
	cout<<" Hello World! ";	//Output of the Program
	
	return 0;
	
}	//body of the main function
OUTPUT OF THE PROGRAM:

Hello World!

Let’s go through each line and the terms used in the above program:

#include

All lines in C++ that begin with the pound (#) symbol are referred to as directives, and they are handled by the preprocessor, which is a program run by the compiler. The #include directive instructs the compiler to include a file, while the #include<iostream> directive instructs the compiler to include an iostream. It informs the compiler to include the standard iostream file, containing declarations for all of the library’s standard input/output functions.

using namespace std;

This is used to import the whole std namespace into the program’s current namespace. The use of the namespace std in a statement is typically looked upon. Importing a namespace effectively pulls all type definitions into the current scope. The standard namespace is enormous. An option to this statement is to use the scope operator(::) to indicate the namespace to which the identifier belongs each time we create a type.

int main()

This line declares the “main” function, which returns data of the integer type. A function is a collection of statements that accomplish a specific function. Regardless of where the main() function is situated in the program, the execution of every C++ program starts with it. As a result, a main() function is required in every C++ program.

Body Of The Program (i.e., Main() function) '{ }': 

The beginning of the main function is indicated by the opening braces ‘ { ‘ and the ending of the main function is indicated by the closing braces ‘ } ‘. Everything between these two curly braces ‘{ }’ comprises the body of the main () function.

cout<< " Hello World! ";

This line commands the compiler to print “Hello World” on the screen. In C++, this line is known as a statement. Every statement is intended to accomplish a specific task. To finish a statement, the semicolon ‘;’ is used. The semicolon character at the end of the sentence shows that the statement is coming to an end. The standard character output device, usually the desktop screen, is identified via the cout command. Everything that comes after the character “<< ” is sent to the output device.

return 0;

This is also a declaration or statement. This statement is used to signify the end of a function and to return a value from it. This statement is used in functions to return the results of the actions that the function has performed.

//Cpp to print the message 'Hello World' on the screen 

This line serves as a comment. A comment is used to display additional program information. There is no programming logic in a comment. When a compiler encounters a comment, it simply passes over that line of code. In C++, every line starting with ‘//’ without quotes OR between /*…*/ is considered a comment by the compiler.

In the above simple C++ program, we’ve printed the message ‘Hello World!’ on the screen. We’ve just provided an overview of each term used in the above program. We’ll learn all these in more detail in the upcoming class.

For now, We hope you’ve understood how to write a simple C++ Program. Remember! This is just the beginning; there is a lot to learn. Stay tuned never to miss any updates. Also, If you’ve any questions or need any assistance, please comment on your query, and we will respond to it at the earliest.

Related Articles

Leave a Comment