Variable Initialization.

Q16. How many ways can a variable be initialized into? Give examples for each types of initialization.

Ans. A first value (initial value) may be specified in the definition of a variable. A variable with a declared first value is said to be an initialized variable. C++ supports two forms of variable initialization at the time of variable definition:

int val = 1001;

int val (1001);

In both cases, val is initialized with s first value of 1001. Following are some more examples of initialized variables:

double price = 214.70, discount = 0.12;

float int a = 0.27;

long val = 25L;

One additional feature of C++, is that it permits initialization of the variables at run time. This is referred to as dynamic initialization. A variable can be initialized at run time using expressions at the place of declaration. For instance, if a variable has been declared as follows:

float avg;

avg = sum/count;

It will initialize avg using the information available at run time, that is, using the values of sum and count known at run time.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: