Basic Structure of C++ Program

Basic Structure of C++ Program

The format of writing programs in C++ is called its structure. The basic structure of C++ program is very flexible. A program in C++ consists of the following parts:

Basic Structure of C++ Program

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

Preprocessor Directive

Preprocessor directives are instructions given to the preprocessor. The preprocessor directives are processed by a program known as preprocessor. It is part of C++ compiler. It modifies C++ source program before compilation. The semicolon is not used at the end of preprocessor directives. The preprocessor directives start with hash symbol #. These directives are written at the start of program.

Preprocessor Directive

Include Preprocessor

Include preprocessor directive is used to include header files in the program. The syntax of using this directive is as follows:

#include <iostream.h>

The above statement tells the compiler to include the file iostream.h in source program before compiling it.

Header Files

Header files are collections of standard library functions and objects to perform different tasks. C++ provides many header files for different purposes. Each header file contains different types of predefined functions and objects. 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.

Header Files

The extension of a header file is h. The include preprocessor directive is used to include header files in programs. The header files are normally stored in INCLUDE subdirectory. The name of header file is written in angle brackets. These files are provided by C++ compiler system

Syntax of C++

The syntax of using header files is as follows:

#include <header_file_name>

The name of header file can also be used in double quotes as follows:

#include "header_file_name"

Example 01

#include <iostream.h>

The above statement will include the header file iostream.h in the program. The word iostream stands for input/output stream. The iostream.h header file contains the definitions of built-in input/output functions and objects Almost all programs use input/output operations so iostream.h header file is included in all programs.

Example 02

#include "math.h"

The above statement will include the header file math.h in the program. This header file contains the predefined mathematical functions.

main() Function

The main() function is the starting point of a C++ program. The control enters main() function and starts executing its statements when the program is run.

main() Function

Each program must contain main() function. A program that has no main() function can be compiled but cannot be executed. Any number of statements can be written in the body of the main() function

The syntax of main() function is as follows:

void main() 

{

body of main function

}

C++ Statements

A statement in C++ language is an instruction for the computer to perform a task. Computer performs these instructions in the same sequence in which they are written in the program.

C++ Statements

The statements of the program are written in curly braces. The curly brace | is called opening brace and is called closing brace. The braces are also known as delimiters. These statements are collectively known as the body of a program. Each statement in C++ language is terminated by semicolon (). It is called statement terminator The compiler generates a syntax error if the statement terminator is not used at the end of a statement.

Example:



Preprocessor Directive  #include<iostream.h>

Main Function    voind main( )

Program Body     {

                                    Cout<<”Hello World!”;

                                     }


#include <iostream.h>

It is the preprocessor directive that includes iostream.h header file. This header file is included in the program because the program displays output on the screen.

void main()

It is the main function of the C++ program. It contains C++ statement. The keyword void before main() function indicates that the function returns no value

cout<<"Hello World!";

It is a C++ statement that displays "Hello World!" on the screen. The cout object is used to display output on the screen. It is written in curly bracket that is called body of main function

Using 'cout'

The cout object is used to display standard output on the screen. The message may consist of strings and values. A string is a set of characters. The cout object is used with the insertion operator <<. Anything written after insertion operator is displayed on the screen.

Token

A token is a language element that is used to form a statement. C++ statement may consist of different tokens.

Keywords:

Keyword is a word in C++ that has a predefined meaning and purpose. The meaning and purpose of a keyword is defined by the language developer

Examples: 

for, double, if, const

Identifiers:

Identifier is the name of a variable or function etc. It is also called user- defined word

Examples: 

Student_age, item20, counter, number_of_character

Constants:

Constant is a quantity that cannot be changed during execution of a program

Examples: 

213,520, 420.7, -2207, 'j'

String Literals:

A collection of characters written in double quotations is called string or string literal.

Examples:

 "This is a string constant", "120", 99-Mall Road, Lahore";

Operators

Operator is a symbol that performs some operation.

Examples: 

+, *, /, -, ?, %

Punctuators

Punctuator is a symbol that marks the beginning or ending of a statement. It is also used to separate two tokens.

Examples:

 [], (), {}, " ", ' ', : , ;

White Spaces

The white spaces are used in programs to increase readability. Different types of white spaces include space, tab and carriage return etc. C++ compiler ignores the white spaces. All statements of a program can be written on one line but it will be difficult to read. The white spaces make the source program more readable. A single statement can be written on two of more lines in order to increase the readability but it will remain same for the compiler.

Post a Comment

0 Comments