Structure of a C++ program
If you are going to learn C++ programming for the first time, then you get many components in a C++ program. Which helps the beginner C++ programmer to create an executable C++ program by combining many components and elements. The C++ program creation process is easy for any programmer. Where you have to face many C++ program elements such as program preprocessor directives, function declarations and main program logic.

So let us know all the components and processes coming in a C++ program in a better way.
C++ Program Preprocessor Directives.
Preprocessor directives are previewed in the top or 1st line of the C++ program file, where C++ preprocessor directives provide all the processes and instructions to any C++ program from start to end program compilation to execution. In C++ programs, these instructions are used to include program libraries, program constant variable data type declaration and other programming steps.
- C++ Header Files (#include) – In any C++ program, header files are used to add external libraries or header files to the program.
- C++ Macro Definitions (#define) – In special C++ programs, macro header files are used to define constants or macros in the C++ program. Which can be replaced as per the existing program requirement.
- C++ Conditional Compilation (#ifdef, #ifndef, #endif) – In C++ programs, conditional header files are used to add or remove parts of the program code based on some particular condition. Generally, they are used in multi-platform program error cross-platform compatibility.
Example of header file in C++ program.
#include <iostream> // It Include the standard input-output stream library
using namespace std; // Allow us to use standard library objects and functions without the ‘std::’ prefix
Namespace declaration in C++ program (optional).
Standard library functions and objects in C++ programs are part of the std namespace file. If you don’t want to type the std:: code line in your C++ program every time you want to use a function or object from the standard library, such as cout, cin, string, etc., in your C++ program, you can manually declare it with the using instruction in your C++ program.
Namespace declaration example in C++ program.
using namespace std; // This allows us to use standard library objects and functions directly in c++
Namespace library in C++ programs is used in small C++ programs. In projects in large C++ programs, the std:: prefix is mostly used to remove namespace conflicts.
Function declarations and definitions in C++ programs.
In a C++ program, you get one or more built-in program functions of many types. The main C++ function is main(). In any C++ program, functions can be declared and defined before or after the main function.
- C++ Function Declaration (Prototype) – Function declaration in a C++ program provides the program compiler with the function signature (function name, function return type, and function parameters or arguments) before defining them in the program.
- C++ function definition – This is the main part of the function declaration in a C++ program. Where the source code logic is applied in a realtime C++ program.
C++ Function Declaration and Definition Example.
int add(int p, int q); // Function declaration in c++
int main() {
int add = total(1, 2); // it Function call in c++
cout << “add is – ” << add << endl;
return 0;
}
// Declare Function definition
int add(int p, int q) {
return a + q;
}
C++ main function.
The main function in a C++ program main() is the entry point or the first used built-in function of every newly created C++ program. When a C++ program starts for the first time, the C++ program execution starts from the main function. main() is the only function in C++ that is required in every C++ program. Generally, the default return data type of main() in a C++ program is int. Which exits the operating system in an integer output in a particular order.
main() example in a C++ program.
int main() {
// c++ program Code to be executed
return 0; // Return an exit status to the operating system
}
In C++ programs, the default return value is 0, whereas 0 indicates proper program execution. If no error is generated in the C++ program execution, the return value is 0.
Variable declarations and initialization in C++ programs.
In C++ programs, program variables are declared and initialized inside the function body. Remember that C++ is a strongly typed programming language. Hence, it is mandatory for the C++ programmer to declare each variable in the program with its data type. For example, int, double, char, etc. Program data type is the variable declaration method.
Variable declarations and initialization in C++ examples.
int p = 1; // Declare an integer variable named p and initialize it with 1
Statements and expressions in C++.
Inside main() or many other program functions in a C++ program, you have many program logical conditional statements which perform the actual action in the C++ program.
- C++ variable assignment (=) – In any C++ program, the variable data type has to be assigned a value.
- Function call – In a C++ program, the function is called or invoked in the program to execute the function.
- C++ (for, while, do-while) loops – In a C++ program, many loop conditions are applied to repeat or execute a particular code block at a particular time.
- Loop conditions (if, else, switch) – In a C++ program, they help in taking decisions according to the particular condition based on the particular condition.
Statements and expressions in C++ examples.
int p = 7; // Declare and initialize p
if (p > 3) {
cout << “\n P is greater than 3” << endl;
}
Comments in C++.
Comments in C++ programs are used to help new and old programmers understand the program source code actual logic or condition. Which makes it easy for anyone to understand the existing program code. In this C++ program, you get the option of two types of comments.
C++ Comments examples.
These indicate single-line comments in the existing C++ program.
They start with //.
These indicate multi-line comments in the existing C++ program.
You Can Enclosed between /* and */.
Always remember that comments in C++ programs are ignored by the program compiler, and they do not affect the execution process of the program.
C++ return statement.
The return statement in C++ programs is used to return the caller program return value in the function. Here, if the function has no other return data type than the void keyword. Here in the main() function, the default return 0; value indicates that your existing program has been launched successfully.
Example of a simple program in C++ programming.
#include <iostream> // Declare Preprocessor directive
using namespace std; // Access C++ standard library features
// c++ Function (argument) declaration
int total1(int p, int q);
int main() {
int x = 1; // Here We Declare and initialize variables
int y = 2;
int total = total1(x, y); // It Call the total1 function
cout << “\n The total of x and y is = \n ” << x << ” and ” << y << ” is = ” << total << endl; // It Display the result
return 0; // default return is 0 for program success
}
// above Function definition
int total1(int p, int q) {
return p + q; // output the total of p and q
}