Structure of C++Program &declaration,Initialization of Variable

Structure of C++ Program with declaration&initialization of variable


Declaration & Initialization of variable in C++ Program:

In order to use a variable in C++, we must declare it, first. The process of specifying the variable name and its data type is called Variable Declaration

The syntax of declaring a variable is: data type  variable
Example: int a; float b; char c; 

Many variables of the same data type can be declared in a single line. In this case following syntax is used.
data type  variable1, variable2, variable3, …, variable_n; 
Example: int a, b, c,d;

[Note: "Semi-colon; must be placed at the end of the variable declaration."] 

Variable initialization in C++ Program:

The process of assigning a value to a variable at the time of declaration is called variable initialization. The = (equal to) sing is used to initialize a variable. 
Example: int a=5; float b= 1.6; char c= ‘a’ ;

Define Structure of C++ Program: 

The format of writing C++ program is called its structure. The structure of C++ program   consists of following parts:  
  1. Pre-processor directives,
  2. Header file,
  3. Main function,
  4. Program Body, and 
  5. Statements.

Now we will explain each of the above one by one.
  

1. Pre-Processor Directives:        

Pre-processor directive is an instruction given to compiler before the execution of an actual program. These are executed before the compilation process. The compiler resolves all these directives before any code is actually generated. This directive gives instruction to a compiler to perform certain tasks before the compilation process. Almost every C++ program contains a pre-processor directive.

The pre-processor directives start with a # symbol. These directives are written at the start of the program. “#include” is most commonly used pre-processor directive. It is used to include the Header File in the program. There are many other preprocessor directives in C++.

2) Header File:

A header file is also called “include file”. A header file is a file that contains the definition of different predefined functions and objects/terms. Header files contain definitions of Functions and Variables. The meaning and purpose of different predefined functions and objects are stored in header files. Header file has an extension "h".

The programmer just uses the pre-defined function but does not explain the meaning and purpose of them to a computer. This task is done by the header file. Thus this saves our precious time and effort. There are different header files in C++. Each contains the definition of a set of some predefined functions.  The name of a header file is written in angle brackets < >. E.g. <conio.h>  

3. Main function:

The main function is the starting point of a C++ program. When the program runs, the controls enter in main function and stars executing its statements. Each program must contain a main function. If a program does not contain the main function it can’t be executed.  Method of writing the main function is: int main ( )  or  void main( ) 

4. Program Body: 

After the main function, the body of a program starts. The program body begins with opening brace { and ends with closing brace }. Program body contains statements.

5) Statements:  

A statement is an instruction to the computer to perform a certain task. In C++ programming each statement ends with semicolon ( ; ). Thus semicolon is called statement terminator.
  

<iostream.h>: 

<iostream.h> stands for input output stream. It is a header file, it contains the definition of objects like cin>> and cout<<.  cin>> is used for taking input while cout<< is used for displaying output.

<conio.h>:

 <conio.h> stands for console input output. It is also a Header file that contains the definitions of many predefined functions like getch (  ), clrscr ( ).       

Mag-post ng isang Komento

0 Mga Komento