“Hello World!” 1st C++ Program And Basic Structure For Beginners

For the beginners, programming is not easy. But on the other hand, it can be the easiest thing in the word just like your 1st program in C++ which is called “Hello World!“. At the start, we will write a code to display only “Hello World!” on the screen. This is the simplest program of C++ and through this program code, we can understand the basic structure of C++. Writing a code in a text editor like notepad is possible but there saving the file with correct extension is necessary i.e(.CPP, .C .CP).

In this post, we will discuss our 1st program in C++ “Hello World!” and its structure to understand it deeply. So let’s start to write out 1st code in C++.

using namespace std;
int main()
cout<<"Hello World!";

Running the program on online IDE or compilers like Dev C++ will show a message on the screen of Hello World!. there are 2 parts which are Header Files and Main Function. further explanation of main function and division in parts is possible but for now, we are gonna focus on these 2 parts of the code.

Basic Structure of C++:

C++ is case sensitive language. If you want to learn then this is the most important rule you have to remember always. A little mistake in lower case or upper case will make your program faulted and you will see errors in it.
Almost every C++ statement (expect class definition and main function) ends with a semicolon. As you can see from the start of the program to end of the program, every line is ending with a semicolon.

In the 1st part, we have lines started with (#) sign which indicates that this line contains directories of C++. In directories, there are pre-defined words and their actions are stored. Above in program, cout<< means to show the message on the screen. How will compiler know that what should I do when there’s an instruction came called cout<<?  These instructions are stored in library files which we include in the program by writing #include<iostream>. At the end of the program, we have getch(); word and we are using this word for holding the screen (using of return 0; is also a good practice for holding screen).  For using getch(); we must add the library of #include<conio.h>. libraries have all information about inputs and outputs of the program.

Why we use namespace std;? Well! std stands for standard and if you don’t declare namespace std in the start then you have to write std:: in every input-output instruction in the whole program. For example, if you don’t declare std in start then for output you will write the statement like this.
std::cout<<“Hello World!”;
By declaring we just need to write.
cout<<“Hello World!”;
Same as the cout statement, every cin>> instruction is also required namespace std. Declaring this at the top is good practice and quite helpful as well.

2nd part of the program is called the main function which is started with int main() where the actual program exists. Each and everything we perform will be done by this part of the program. We can set a model of instruction in class or structure which is used in Objection Oriented Programming but to execute that model of instructions, we must define some instruction into the main function of C++. Every function in C++ starts with the type and name of the function as we wrote in above program. To write instructions, we must type opening braces { and closing braces } and every line of code must between {} braces.  Any type of instruction written out of braces of the main function will not be executed.

I hope you will understand the 1st simple program and its structure. As being a beginner, it’s difficult to understand the code in the start but when you got grip on it, its become the piece of cake.

Leave a Comment