L3: Variables and Types
Q1. What does a computer do when it executes a variable declaration statement ? Explain with an example.
The general syntax for a variable declaration is datatype variablename . When such variable declaration statement is executed, the computer assigns required memory for that variable depending on the datatype and associates the name with the assigned memory location. We know that, variables of different datatypes require different sizes of memory to store the data. For example, the computer assigns 32 bits of memory when it executes the variable declaration statement int count and links that memory location with the name count. Similarly, in the execution process of variable declaration double radius the computer assigns 64 bits of memory and associates that memory location with name of the variable radius.Q2. How to declare an integer variable named 'age' with the value 45 ?
int age;age = 45;
you can also combine these two lines and write in a single line as follows:
int age=45;
Q3. How to declare a double variable named 'weight' with the value 5.37 ?
double weight=5.37;Q4. How to declare a float variable named 'weight' with the value 5.37 ?
float weight=5.37F;We have discussed in the lecture that, the default types of real numbers are double and we need to append F or f to make it a float. Hence, F is appended in the declaration and 5.37F is written.