A variable is used to store values. Each variable is stored in memory location(s). It can be of any data type such as integer, char, float, etc. A variable holds a single value at a time of its type; that is, values of variable vary, and the programmer can change the value of the variable.
The variable must be declared before they are used in the program. Once declared (1) the compiler obtains the variable name and (2) the compiler is notified about the data type of the variable being declared and helps in allocation of the memory. The variable can also be declared before main() and such variables are called external variables.
In C, all the variables must be declared in the declaration part. Hence, each time when one needs to declare a variable the programmer should go back to the beginning of the program.
C++ permits declaration of variables anywhere in the program. This makes the programmer more comfortable to declare the variables and need not go back to the beginning of the program. The declaration of variable consists of name of data type and variable list as follows.
The syntax of declaring a variable is as follows:
The int, char, float, and double are keywords to represent data types. Commas are used as separators for multiple variables.
When a variable is declared, appropriate number of bytes is reserved for that particular variable in the random access memory. Bytes are filled with garbage values if the user does not assign them a value. If the user performs operations without initializing the variable, the result will be unexpected. Hence, before using a variable it is essential to initialize it. Assigning a value to the variable is called as initialization. When a value is assigned to the variable, the garbage value is removed and replaced with the given value.
Here, the integer variable x is declared and not initialized. When the variables are not initialized, neither they are set to zero by the compiler nor they remain empty. The variables contain garbage values. The user cannot predict the garbage value, which is system dependent. The garbage values differ for each system.
If you try to print the value of x in C, the statement printf(ā%dā,x) will display the garbage value (-29281 on my system). The cout<<x in C++ will display the value 0. If you use both these statements one after another, the value displayed will be 0.
4.5 Write a program to display value of uninitialized variable using cout statement.
Explanation: In the above program, the first cout statement displays the value of x zero; the second statement displays the value of x as 5; and the third statement displays the value 6. In the third statement x is incremented first and then added to 5. This program is compiled under compact memory model. If the program is compiled and executed under any other memory model the resultant value will be garbage.
All the operations performed in the above program are directly put in the cout statement. If the above program is executed with the printf() statement, the program will display the garbage values.
The following program displays garbage value of the variables:
4.6 Write a program to display garbage value of a variable.
Explanation: In the above program, 5 is added to variable x and assigned to y. One may expect that the value of y to be 6. Here, the value of y is displayed as a garbage value. From the above program it is clear that if the variable is not initialized, it will be assigned to a garbage value.
In the afore-mentioned program, the statement y=5+(++x); it uses variables x and y. The variables x and y are not initialized. The variable y has no effect on the operation because it is at the left-hand side and the result of the expression is assigned on the right-hand side. The variable x holds a garbage value. Integer 5 is added to the garbage value of x and stored in the variable y. Hence the value of y displayed is 1134.
Initialization of variable can be done at the place where it is declared or anywhere in the program before their use. Variables declared can be assigned or initialized using assignment operator ā=ā. The declaration and initialization can also be done in the same line.
Syntax: variable_name = constant;
data_type varaible_name= constant;
x=2; where x is an integer variable.
Figure shows an example of initialization of a variable.
Initialization of variable
data-type variable_name = value;
int k = 5;
In the above example, variable k of integer type is declared. The value 5 is stored in it.
In the above example all the variables a, b, c, and d are initialized to 5. An example is illustrated as follows:
4.7 Write a program to initialize more than one variable at a time.
Explanation: In the above program, variables, a, b, c, and d are declared. In next statement, all four variables are initialized with a value 5. First the value 5 assigned to variable d, then the value of d is assigned to c, c is assigned to b, and finally b is assigned to a.
Initialization of variables
Here the variables are local and they are stored in the stack. Stack is the portion of the random access memory (RAM). Generally it is located at end of RAM to avoid overlapping of program and data memory. Figure simulates this assignment.
The declaration and initialization of variable in a single statement at any place in the program is called as dynamic initialization. The dynamic initialization is always accomplished at run-time. Run-time means execution of program. Dynamic refers to a process carried out at run-time, for example dynamic initialization, dynamic memory allocation etc. The C++ compiler allows declaration and initialization of a variable at any point in the program. In C, initialization of variable can be done at any point but the variable must be declared at the beginning of the program illustrated as follows:
4.8 Write a program in c to demonstrate declaration and initialization of a variable.
Explanation: The above program is executed with C compiler. The variables area and r are declared at the beginning because declaration in C is compulsorily done at the beginning. The multiplication of 3.14 and the variable r is assigned to the variable area. This assignment is done within the program. Thus, from the above program it is demonstrated that in C variable declaration is done at the beginning and initialization can be done at any point in the program.
4.9 Write a program in c++ to demonstrate dynamic initialization.
Explanation: In the above program, the variables r and area are declared within the program. The declaration and initialization of a variable area is done in the single statement within the program. Consider the following statement.
In the above statement, float variable area is declared and product of 3.14*r*r is assigned to the variable area. The assignment is carried out at run-time. Such type of declaration and initialization of a variable is called as dynamic initialization.
4.10 Write a program to read two integers through the keyboard. Declare the variables in C++ style.
Explanation: In the above program, the variables num and num1 are declared within the program and not at the beginning of the program. Once declared, they are used with cin statement that reads two integers and stores them in the variables num and num1. The cout statement displays entered values on the screen.
4.11 Use dynamic initialization and display an alphabet.
Explanation: In the above program, variable z is initialized with 102. The ASCII value of 102 is f; hence, the output is f.
4.12 Write a program to calculate length of the string. Use run-time declaration and initialization of variables.
Explanation: In the above program, the cin statement reads the string through the keyboard. The strlen() function is used to determine the length of the given string. The strlen() function calculates the length of the string and returns the length to variable len. The variable len is declared and value returned by strlen() is assigned to variable len. To avoid separate statements for declarations and initializations, both statements are combined as one statement. Declaration and initialization are carried out in one statement int len=strlen (name).