Structure of C++ Program

Basic Structure of a C++ Code

Here, in this page we will discuss the basic structure of C++ Code.
In C++, a program is divided into the following three sections: Standard Libraries Section, Main Function Section and Function Body Section. Here we would learn about each of them in detail with proper examples and explanation.

structure of c++ program

C++ Code Representing the Structure

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

Header Files

In the program above,  #include is an example of a header file.

Definition – Header files contain definitions of Functions and Variables, which is imported or used into any C++program by using the pre-processor #include statement. Header file have an extension “.h” which contains C++ function declaration and macro definition.

Here iostream contains the definitions of input output statements, that are used to print or scan at run time.

NameSpaces

Namespace is a feature added in C++ and not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it. Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.

Just remember, name spaces are used to allow re-declaration of variables with same name in a scope.

using namespace std; – enables this.

Main Function

By design, every C++ program must be enclosed in a function int main() by default it is the first function that is called automatically at run time.

Here the function main() is of return type int, thus return an integer value; at the very end causes the program to execute without any error.

Cout function

cout <<“Hello, World!”;

Here, << which is called as insertion operator, it basically inserts the object on its right to its left and cout is predefined function to print value on the screen.

Keywords in C++

Keywords are predefined reserved identifiers that have special meanings. They cannot be used as identifiers in your program. The following keywords are reserved for C++. Names with leading underscores.

Keywords in C++ program

asm else namespace template
auto enum new this
bool explicit operator throw
break export private true
case extern private try
catch false public typedef
char float register typeid
class for reinterpret_cast typename
const friend return union
const_cast goto short unsigned
continue if signed using
default inline sizeof virtual
delete int static void
do long static-cast volatile
double main struct wchar_t
dynamic_cast mutable switch while

Identifiers

A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).

  1. Identifiers can’t start with a digit, only underscore(_) and alphabets with no whitespaces.
  2. Identifiers are case sensitive, value and Value are two distinct identifiers.
  3. You can’t use a keyword as an identifier.

Comments in C++

  • Single line Comments – // I will only extent to single line
  • Double line comments – /* i will span to various ……………… different lines*/

Comments are used to increase the understandability enhancing readability of the code. Generally, comments are written to help other coders, who are working on same application and want to understand the code written by someone else.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription