C++ program’s basic structure

C++ program basic structure

The C++ program is a collection of instructions that are written in a specific format and is called the C++ Program’s basic structure.  Every program written in C++ follows this structure.

Let’s have look at the basic structure of the C++ program

//C++ program's basic structure
using namespace std;
int main()
	cout<<"Hello World";

1: //C++ program’s basic structure

A line that starts with a “//” symbol is called a single-line comment. Which does not run during the execution of the program. It is written for the convenience of the programmer to make the code easier to understand. If you want to get more information about comments, open this link.

2: #include <iostream>

It is a preprocessor directive that always begins with the “#” symbol to connect pre-written code or libraries to your C ++ program file. The preprocessor directive itself is not a C++ instruction that’s why it does not end with a semicolon “;”.

As in the above program, the preprocessor “iostream” library is integrated with the program. This library provides input and output functionality to the program having pre-written functions cin, cout, etc.

3: Using namespace std;

namespace “std” refers to a block where the Standard c++ library is declared. It is a part of C++ programming to avoid conflicts while using multiple libraries within the same program.

Basically, the “namespace” is a block having a specific name that gives scope to identifiers declared inside that block. We can give the same name to the identifier within different namespaces in a single program. 

4: int main ()

main() is a function. Before we discuss the main function, let’s discuss the functions. A function is a block of statements in a c++ program that runs with a function call. On the other hand, the main function is a special function that automatically runs first during the execution of the program. It gains all the control over the c++ program execution. The statements inside this function begin to run one by one.

5: {}

Opening curly braces and closing curly braces represent the body of the function. All the statements related to the function are written inside them.

6: cout<<” Hello World”;

This is a complete “cout” statement in C++. It prints “hello world” on the computer screen. All C++ statements are always ended with a semicolon “;”.

More Related Articles