Structure of C++ program with simple C++ program

structure-of-c-plus-plus-program

In this article, we will learn about the structure of the C++ program with a simple c++ hello world program.First we will start from the structure of c++ program.

Structure of c++ program

C++ program is structured in a specific and particular manner. In C++, a program is divided into the following three sections:

  1. Standard Libraries Section
  2. Main Function Section
  3. Function Body Section

Let’s take a example to understood the above three sections.

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!" << endl;
  return 0;
}

Above the simple hello world c++ program, now we will understand each and every section.

Standard libraries section

#include <iostream>
using namespace std;
  • #include is a specific preprocessor command that effectively copies and pastes the entire text of the file, specified between the angle brackets, into the source code.
  • The file <iostream>, which is a standard file that should come with the C++ compiler, is short for input-output streams. This command contains code for displaying and getting an input from the user.
  • namespace is a prefix that is applied to all the names in a certain set. iostream file defines two names used in this program – cout and endl.
  • This code is saying: Use the cout and endl tools from the std toolbox.
  • Here cout is used for printing the code where as endl is used for next line.

Main Function Section

int main() {}
  • The starting point of all C++ programs is the main function.
  • This function is called by the operating system when your program is executed by the computer.
  • { signifies the start of a block of code, ​and } signifies the end.

Function body section

cout << "Hello World" << endl;
  return 0;
  • The name cout is short for character output and displays whatever is between the << brackets.
  • Symbols such as << can also behave like functions and are used with the keyword cout.
  • The return keyword tells the program to return a value to the function int main
  • After the return statement, execution control returns to the operating system component that launched this program.
  • Execution of the code terminates here.
5 1 vote
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x