C++ Program Structure

C++ is a very popular programming language. Every C++ program has a general structure that it must follow.


Here is common structure of a C++ program.

C++ Program Structure
C++ Program Structure

Header Section

The header section is used for

  1. Including header files
  2. Declaring global variables
  3. Declaring functions
  4. Defining constants

You can divide C++ functions into two categories – Builtin functions and User-defined functions. Builtin functions are written in a header file that ends with a .h extension. In other words, builtin functions comes with the C++ compiler located in a directory called Include.

User-defined functions are written by programmers. You can write your own codes and store them in the source file for C++ that ends with extension (.cpp).

Syntax to include header file in a C++ program

#include "iostream.h" 


#include <iostream.h>

When you use double quotes the compiler look for the header file in the directory where the C++ program located and then look for file in Include directory that contains all the headers. Otherwise, if we use < ….> , then compiler look for the file only under Include directory.

The header section is a global section because it is outside the main program. You can declare functions (user-defined) with global scope so that all other functions can call it anytime.

void add() ;

int calculate();

You can declare global variables which is used by the main program components such as functions and expressions.

int number;

char name;

You can specify a macro or a numeric constant value in the header section. The value of a numeric constant does not change during the execution of a program.

Syntax for declaring numeric constant

#define MAX 10

#define PI  3.14

The Main Function

The main function is where your program gets executed first. The main function has a default return type of integer in C++. It calls other functions while executing.

void main()

            statement 1;

            statement 2;



            statement n;


The two braces – { and } indicate a block of statements and main() like every other function has a block of statements. The block is the entire C++ program and when it has finished execution, returns an integer value.

If you do not want main to return any value, use the keyword void main() to indicate this.

Other Functions

A user-defined function is a function that performs some task for the user in the program. It is different from built-in functions because user has the control to change the function anytime.

The programmer declares the function in the head section or in the main body of a C program. The declared function has definition that defines what the function does. The best practice is to keep the function definition after main() function. But, it is not necessary.

Putting it all together

The following is an example of C++ program structure.

#include <iostream.h>

#define NUM 100

void main()


        int x = 20;

        int sum = 0;

        sum = x + NUM;

        cout << sum << endl;





Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.