What is the basic structure of a C++ program?

Structure of a C++ program

A 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

For example, let’s look at the implementation of the Hello World program:

#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}

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.

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.

Coding example

Now, let's conclude this topic with an extended coding example.

#include <iostream>
using namespace std;
// Function to calculate the factorial of a number
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
// Function to print a Fibonacci series up to a given number of terms
void fibonacci(int n) {
int first = 0, second = 1, next;
cout << "Fibonacci Series up to " << n << " terms: ";
for (int i = 1; i <= n; ++i) {
cout << first << " ";
next = first + second;
first = second;
second = next;
}
cout << endl;
}
int main() {
// Example of using cout to print "Hello World!"
cout << "Hello World!" << endl;
// Example of using cout to print the factorial of 5
cout << "Factorial of 5 is: " << factorial(5) << endl;
// Example of using cout to print the Fibonacci series up to 10 terms
fibonacci(10);
return 0;
}

  • Standard Libraries Section:

    • Lines 1&2: This section includes the necessary header file <iostream> for input-output operations and utilizes the using namespace std directive to avoid prefixing standard library elements like cout and endl with std::.

  • Function body section:

    • This section defines two functions: factorial() and fibonacci(). factorial() computes the factorial of a given number, while fibonacci() prints the Fibonacci series up to a specified number of terms. These functions are invoked in the main() function for demonstration purposes.

  • Main Function Section:

    • Lines 25-36: In this section of the code, The main() function serves as the entry point for the program execution. It demonstrates the usage of cout to output "Hello World!", calculate the factorial of 5 using the factorial() function, and print the Fibonacci series up to 10 terms using the fibonacci() function.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved