Wednesday, October 26, 2011

Type Declaration Instruction

This instruction is used to declare the type of variables being used in the program. Any variable used in the program must be declared before using it in any statement. The type declaration statement is written at the beginning of main( ) function.
Ex.: int bas ;
float rs, grosssal ;
char name, code ;
There are several subtle variations of the type declaration instruction. These are discussed below:

(a) While declaring the type of variable we can also initialize it as shown below.
int i = 10, j = 25 ;
float a = 1.5, b = 1.99 + 2.4 * 1.44 ;

(b)
The order in which we define the variables is sometimes important sometimes not. For example,
int i = 10, j = 25 ;
is same as
int j = 25, j = 10 ;
However,
float a = 1.5, b = a + 3.1 ;
is alright, but
float b = a + 3.1, a = 1.5 ;

is not. This is because here we are trying to use a even before defining it.

(c) The following statements would work
int a, b, c, d ;
a = b = c = 10 ;
However, the following statement would not work
int a = b = c = d = 10 ;
Once again we are trying to use b (to assign to a) before defining it.

No comments:

Post a Comment