Basic Syntax:

The formatting of writing C++ program is called its structure. The basic structure of a C++ program is very flexible. It increases the power of the language. It consists of the following parts:

  • Preprocessor directive
  • Main () function
  • Program body (C++ statements)

 

C++ Program

c++-program

In the above program

  • First line is preprocessor directive to include a header file <iostream>. The preprocessor directives are the commands that give instructions to C++ preprocessor. Preprocessor is a program that modifies source program before compilation. The preprocessor directive starts with #.
  • This line tells the compiler to use the std namespace.
  • The third line is main function. The main () function is the place where execution of C++ program starts. When the program is executed the control enters main () function and starts executing its statements.
  • The statements of the program are written in curly brackets. The curly bracket { is the opening bracket and } is the closing bracket. The braces are also known as delimiters. These statements are collectively known as the body of a program.
  • Every statement in C++ program is terminated with a semicolon (;). The compiler generates an error if any statement is not terminated by semicolon.

 

 Preprocessor directive

Preprocessor directive is an instruction given to the compiler before the execution of actual program. It is also known as compiler directive. The preprocessor directives are processed by a program known as preprocessor. It is a part of C++ compiler it modifies C++ source program before compilation. The semicolon is not used at the end of preprocessor directives.

 

Header files

Header files are the collection of standard library functions to perform different tasks. There are many header files for different purposes. Each header file contains different types of predefined functions. Many header files can be included in one program. The header file must be included in the program before calling any of its functions in the program.

Some examples of header files are as follows:

<iostream>, <conio.h>, <string>, <cmath>, etc.

 

Main function

A function performs a single well-defined task. Every C++ program must have a main () function which is the first section to be executed when the program runs. When the program is executed, the control enters main () function and starts executing its statements. The word void before the function means that this function does not return a value.

 

C++ statements

A statement in C++ language is an instruction for the computer to perform a task. The statements are written in curly brackets. Each C++ statement is terminated with a semicolon.