2 Variable
2 Variable
UNIT1: Variable
Covered Topics
Index(Page no) Topics
1 Variable
2 Properties of variables
2 Various syntaxes of the variable declarations
2 Types of statements
2 Declarative statement
3 Executable statement
3 Pre-processing statements
3 More about semicolon
3 Things to be know about the variables
4 Types of variables(primitive variable vs pointer variables)
4 Primitive variables
4 Pointer variables
4 Based on purpose and positions variables again divided into two
categories(local variable vs global variable)
4 Local variable
4 Memory representation of stack area
6 Rules of local variables
11 Global Variables
13 Scope Resolution Operator (::) operator
13 Difference between local variable and global variable
14 Naming rules of variables
15 Practice1: variables
16 Answer - Practice1: variables
16 Practice2: variables
18 Answer – Practice2: variables
18 Practice3: variables
20 Answer – Practice3: variables
21 Solution:- Practice1: variables
22 Solution:- Practice2: variables
25 Solution:- Practice3: variables
Variable
A variable is nothing but it is logical name of the memory which can store the data during execution
of the program.
Variable acts like a container
Consider the given mathematical expression
2x – 3y + 5
In the mathematics this type of expression we had seen many times
In this expression 2,3,5 are the constant and - + are the operators and x, y are the variables.
As we know the value of the constant can’t be changed but we can change the value of any
variables any number of times.
In this expression we can put the different-different values of variables x,y. Most common way of
solving this expression mathematically like that way…
2|Page C Language Mat erial by Girraj Sharma
Properties of variables
All variables are having three common properties
1. Variable name
2. Variable value
3. Variable address
In this diagram salary is a variable name, 25000 is the variable value and 1000 is the memory
(byte) address of this variable.
Types of variables
Based on types of stored contents variables are divided into two categories
a. Primitive variables
b. Pointer variables
Primitive variables:
Any variable that can store the values known as primitive variable
Ex: x = 10, y = ‘a’, z=2.5
Syntax of primitive variables:
datatype variable = value;
Pointer variables:
Any variable that can store the memory address known as pointer variable
Syntax:
datatype * variable = address;
Based on purpose and positions variables again divided into two categories
1. Local variables
2. Global variables
Local variable
Any variable which is declared inside body of function or block known as local variables
Ex: void main()
{
int x = 10;
}
In this example variable x is declared inside the body of main() function therefore x is a local variable.
Q: whenever memory is allocated to the local variables?
Ans: Memory is allocated to the local variables whenever execution control comes inside a function
or block.
Q: whenever memory deallocation is performed to the local variables?
Ans: Local variables got deallocated(removed) from the memory whenever execution control goes
outside the block.
Q: In which memory block local variables got created?
Ans: By default all local (auto) variables are created inside stack area.
Memory address of stack can be ranged between 0 to 65535. In case of 16bit C compiler int type
variables will takes two bytes therefore irrespective of int type of variables we are showing each block
size 2bytes according to blow diagram.
5|Page C Language Mat erial by Girraj Sharma
Since we can’t guess the actual memory address of any variable because address of any variable
may change execution to execution and compiler to compiler.
Note: For the sake of execution of any program just program diagram representational purpose we
will going to assuming base address of first variable in the program is always 100.
Memory allocation of all local variables will be performed always top of the stack. For more
clarity refer the blow diagram.
Ex:
Assume if the memory address of x is 100, then address of y is 98 and address of z is 96 (in 16bit c
compiler). Because firstly memory is allocated to the x and then y is created top of the x, similarly z is
created top of the y according to stack property.
Note: Use %u inside a printf() function in order to displaying address of any variable.
Example1:
6|Page C Language Mat erial by Girraj Sharma
Memory is never allocated to the unused local variables even though we have provided the
declaration of that unused variable.
In this program variable y is declared but never used in the program. So the difference between the
address of x and z is 2 (100-98). If y also takes the space between x and z, then we will get the
difference between the address of x and z as 4.
Declarations of local variables are allowed only beginning of the block. Variable declaration is
not allowed after the executable statement.
Ex: void main()
{
int x = 10; //valid
7|Page C Language Mat erial by Girraj Sharma
int y; //valid
{
char a = '#'; //valid
}
}
Output: Nothing is printed
Here variable x,y are declared beginning of the main() block. And variable a is declared at the
beginning of the local block. So the all declarations are valid.
Ex: void main()
{
int x = 10;
x = x * 5;
int y = 20; //invalid
}
Output: Error, Declaration of y is provided after executable statement which is not allowed in C.
Ex: void main()
{
int x = 10;
;
int y = 20; //invalid
}
Output: Error
Inside any function dummy semicolon considered as executable statement, therefore after dummy
semicolon variable declaration is not allowed.
Ex: void main()
{
}
;
int x = 10; //valid
Output: Code will compile successfully, but in this program nothing to display.
Outside function dummy semicolon is a valid declarative statement, so variable declaration is
allowed after dummy semicolon outside the function.
Declaration of local variables will include the datatype and storage class information.
Declaration of local variable is nothing but it is information to the compiler about the datatype
and storage classes.
Within a block duplicate variable names are not allowed.
Ex: void main()
{
int x = 10; //valid
float x = 2.5; //invlaid
}
Output: Error
In this example we are providing declaration of two variables using same name x so it will not be
allowed by the compiler.
But two or more variables can have same names in different-different blocks/function.
Ex: void main()
{
int x = 10; //valid
{
float x = 2.5; //vlaid
8|Page C Language Mat erial by Girraj Sharma
}
}
Output: Nothing printed
We can provide more than one variable declaration either by using separate statement or by using
single statement.
Ex: void main()
{
int x;
int y;
int z;
}
Ex: void main()
{
int x,y,z; //valid
}
Default value of any local variable is unknown or garbage.
Garbage: Any value that cant be predict known as garbage value.
Ex: void main()
{
int x,y,z;
printf("%d %d %d",x,y,z);
}
Output: garbage garbage garbage
In this example we cant predict the value of x,y,z.
We can assign a value to a local variable either at the time of declaration or after declaration
anywhere in the program.
Ex: void main()
{
int x = 10,y,z;
z = 20;
printf("%d %d %d",x,y,z);
}
Output: 10 garbage 20
In this case y is not assigned so the value of y is garbage.
The scope of local variable is very less only within a current block or inside their local block.
Scope: A place in the program where we can access/used the variables known as scope of that
variable.
Any local variable cant be accessed outside the block/function in which the local variable is
declared.
Outer block local variables can be accessed inside outer block as well as inside their local block.
But inner block local variables cant be accessed by the outer block.
Ex: void main()
{
int x = 10;
{
int y = 20;
printf("%d %d",x,y); //valid
}
}
Output: 10 20
Ex: void main()
{
9|Page C Language Mat erial by Girraj Sharma
int x = 10;
{
int y = 20;
printf("%d %d",x,y); //valid
}
printf("%d ",x); //valid
printf("%d ",y); //invalid
}
This code will be compiled because variable y cant be accessed outside the inner block because
its declaration is provided inside local block.
It is valid to initialize the local variables during its declaration by using the value of another local
variable which is already declared.
Ex: void main()
{
int x = 10;
int y = x; //valid
}
Ex: void main()
{
int a = 10, b = a; //valid
}
Ex: void main()
{
int a = b, b = 10; //invalid
}
This code is invalid because b is used by the variable a, we need to provide declaration b before
the variable a.
Ex: void main()
{
int p = q; //invalid
int q = 10;
}
This code is invalid because q is used before the declaration of q.
Explanation:
(1) Execution of main() function is started.
(2) Variable x is created inside stack memory with initial value 10.
(3) Current value(10) of x is printed on the monitor.
(4) Current value of x is replaced by the new value 5 inside a memory, so the x becomes 5.
(5) Again current value of x printed on the monitor as 5 so the final output is 10 5.
10 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Tips: To understand the process of this program go through the point (1) to (11) one by one.
Explanation:
(1) Execution is started
(2) Memory block is reserved to the x with initial value 10.
(3) On the top of x memory is allocated to the y with initial value 20.
(4) Value of x(10) and y(20) is printed on the monitor by the printf() function.
(5) Address of x(100) and y(98) is printed on the monitor by the printf() function.
(6) y is deallocated(free) from the memory, so the now y is no more on the memory.
(7) z is created on the top of x, because location 98 already deallocated so the location 98 is reusing by
the z. therefore address of z is exactly same as of y.
(8) x(10) and z(20) is printed on the monitor by the print() function.
(9) Address of x(100) and z(98) is printed on the monitor by the printf() function.
(10) Variable z got deallocated
(11) Variable x got deallocated, after compleation the execution now the location 100 and 98 is
free.
Ex: void main()
{
int x = 10;
{
int y = 20;
printf(" %d",x);
printf(" %d",y);
}
printf("%d ",x);
11 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Note: By default local variables having auto storage class. Even we can declare local variable using
register storage class also.
Note: If any local variable is declared by using register storage class then memory is allocated inside
the cpu register memory rather than the stack.
Global Variables:
Any variable declared outside the functions directly in the source file known as global variables.
Operating system allocates the memory space for all the global variables at the time of code
loading.
Even execution of main() function is also performed after completing the allocation process of
global variables.
Ex: int x = 10; //x is global variable
void main()
{
int a = 20; //a is local variable
{
int b = 30; //b is local block
}
}
int y = 20; //y is global variable
Q: whenever memory is allocated to the global variables?
Ans: Memory allocation of all the global variables will be performed at the beginning of execution of
program. Of course before calling the main() function also.
Explanation:
(1) Memory is allocated to the x inside data area with initial value 10.
(2) Memory is allocated to the y inside data area with initial value 20.
(3) Execution of main() function is started.
(4) Memory is allocated to the local variable a inside stack with initial value 1.
(5) Value of x,y,a is displayed on the monitor by the printf() function.
12 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Global variable declaration can be provided anywhere in the program before or after the main()
function. If any global variable is declared after the main() function then it can’t be accessed
inside the main() function but such type of global variables can be accessed only inside those
functions which is declared after the global variables.
Ex: int x = 10; //valid
void main()
{
printf("%d ",x); //valid
printf("%d ",y); //invalid
}
int y = 20; //valid
void fun()
{
printf("%d ",y); //valid
}
Explanation: x is available in local scope as well as global scope, local x will gets high priority
over the global variable x, in local scope value of x is 1, so value of x is print as 1. And y is not
available in local scope so the value of global variable y is printed as 10.
If local and global variables are having same names then by default local variables will gets high
priority in such cases we can access the global variables explicitly by using scope resolution
operator (::).
Explanation: In this example variable x is available as local variable as well as global variable.
Whenever we are accessing x directly then definition (value) of x searches in local scope. By in
case of ::x, definition of x directly searches in global scope so value of ::x is 1.
Tips:
Predefine macro constants can be used as a variable name, but it is never recommended to use
predefine macro constant names as a variable.
Valid name: NULL, EOF
It is valid to use predefine function name as a variable name but it is never recommended to use
library(predefine) function names as a variable names.
Valid name: printf, scanf, clrscr, getch, sqrt
Even though we can use any random names for variables (x,y,abc,a1,a2) but it is recommended to
use meaning full or self descriptive variable names
Ex: age,salary,marks,name,phone,email
15 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Practice1: variables
Q1: Which is false about local variables?
a. A variables declared inside block
b. A variables declared inside function
c. A variables declared outside function
d. A variables declared as function parameters
Q2: Which is true about local variables?
a. Created inside stack
b. Created inside register
c. Created inside data area
d. Created inside method area
Q3: Which is true about local variables?
a. We must initialize explicitly
b. Default value is zero
c. Default value is garbage
d. We cant change their value
Q4: Which is true about local variables?
a. Memory is allocated at the time of loading inside stack
b. Memory is allocated at the time of loading inside data area
c. Memory is allocated inside register whenever execution control comes inside block
d. Memory is allocated inside stack whenever execution control comes inside block
Q5: Which is true about global variables?
a. A variables declared inside block
b. A variables declared inside function
c. A variables declared outside function
d. A variables declared as function parameters
Q6: Which is true about global variables?
a. Created inside stack
b. Created inside register
c. Created inside data area
d. Created inside method area
Q7: Which is true about global variables?
a. We must initialize explicitly
b. Default value is zero
c. Default value is garbage
d. We cant change their value
Q8: Which is true about global variables?
a. Memory is allocated at the time of loading inside stack
b. Memory is allocated at the time of loading inside data area
c. Memory is allocated inside register whenever execution control comes inside block
d. Memory is allocated inside stack whenever execution control comes inside block
Q9: We must initialize global variables?
a. true
b. false
Q10: Which is false about variable or identifier naming?
a. Can contains alphabets, digits, underscore
b. Names are case sensitive
c. Can start with digits
16 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
Practice2: variables
Q1: Which is invalid identifier names?
a. __
b. __xyz__
c. 1s
d. student#marks
Q2: Which is invalid identifier names?
a. m1
b. 1m
c. _1
d. _Hello25
Q3: What is the output?
void main()
{
int XYZ=10,xyz=20,xYz=30;
printf("%d",XYZ+xyz);
}
Options:
a. Error
b. 10 20
c. 10
d. 30
Q4: What is the output?
void main()
{
int does=10,did=20,do=30;
printf("%d",does+did);
}
Options:
a. 10 20
b. 30
c. 10
d. Error
Q5: What is the output?
void main()
{
int x=10;
float x = 2.5;
printf("%f",x);
}
Options:
17 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
a. 10
b. Error
c. 2.5
d. 2.500000
Q6: What is the maximum length of an identifier name?
a. 8
b. 16
c. 32
d. No limit
Q7: What is the output?
void x()
{
printf("x");
}
void main()
{
int x = 10;
printf("%d ",x);
x();
}
Options:
a. 10 x
b. x x
c. 10 10
d. Error
Q8: What is the output?
int x = 10;
x = x * 2;
void main()
{
printf("%d",x*2);
}
options:
a. 10
b. 20
c. Error
d. 40
Q9: If address of x is 100 and then what is the output(In 16bit compiler)?
void main()
{
int x=10;
int y=20;
int z=30;
printf("%u %u %u",&x,&y,&z);
}
Options:
a. Can’t determine
b. 100 98 96
c. 100 102 104
d. 100 104 108
Q10: If address of x is 100 and then what is the output(In 16bit compiler)?
18 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
void main()
{
int x=10;
int y=20;
int z=30;
printf("%u %u",&x,&z);
}
Options:
a. Cant determine
b. 100 96
c. 100 104
d. 100 108
Practice3: variables
Q1: If address of x is 100 and then what is the output(In 16bit compiler)?
void main()
{
int x=10;
int y;
int z=30;
printf("%u %u",&x,&z);
}
Options:
a. 100 98
b. 100 96
c. 100 102
d. 100 104
Q2: If address of x is 100 and then what is the output(In 16bit compiler)?
void main()
{
int x=10;
int y;
int z=30;
printf("%u %u",&x,&z);
y = 20;
}
Options:
a. 100 98
b. 100 96
c. 100 102
d. 100 104
Q3: What is the output if program is executed based on stack property?
void main()
{
19 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
int x=10;
{
int y=20;
printf("%d %d ",x,y);
}
{
int z;
printf("%d %d",x,z);
}
}
Options:
a. 10 20 10 20
b. 10 20 10 garbage
c. Error
d. Multiple blocks not allowed
Q4: Which is false about local variable and scope?
Options:
a. Scope is a place where variables are visible and can be used for any purpose.
b. Scope is a time period between memory allocation and memory deallocation.
c. Variables cant be accessed outside current block.
d. Outer block local variables can be accessed their inner block.
Q5: Which is false about variable declarations?
Options:
a. Within a block variables cant have same name
b. Local variables can be declared anywhere in the function.
c. Local variable declaration should be top of the function
d. Global variables can be declared anywhere from outside function
Q6: What is the output?
void main()
{
int x = 10;
{
printf("%d ",x);
int x = 20;
printf("%d ",x);
}
printf("%d ",x);
}
Options:
a. 10 20 10
b. 10 20 20
c. 10 10 20
d. Error
Q7: What is the output?
void main()
{
int x = 10;
{
int x = 20;
printf("%d ",x);
x = 5;
20 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
}
printf("%d ",x);
}
Options:
a. Error
b. 20 5
c. 20 10
d. 20 20
Q8: What is the output?
int x = 1;
void main()
{
int x = 2;
printf("%d ",x);
}
Options:
a. 1
b. 2
c. Error
d. Compiler Dependent
Q9: What is the output?
int x = 1;
void main()
{
int x = 2;
printf("%d %d",x,::x);
}
Options:
a. 1 1
b. 2 2
c. 1 2
d. 2 1
Q10: What is the output?
void main()
{
int x = 1;
{
int x = 2;
printf("%d %d",x,::x);
}
}
Options:
a. Error
b. 2 2
c. 1 2
d. 2 1
4 B 9 D
5 B 10 A
void main()
{
int XYZ=10,xyz=20,xYz=30;
printf("%d",XYZ+xyz);
}
Options:
a. Error
b. 10 20
c. 10
d. 30
Solution: As we know variable names are case sensitive so the XYZ, xyz, xYz are three distinct
variable names. And printf() has displayed addition of XYZ + xyz. So the 10+20 result is 30.
void x()
{
printf("x");
}
void main()
{
int x = 10;
printf("%d ",x);
x();
}
Options:
a. 10 x
b. xx
c. 10 10
d. Error
Solution: If variable name and function names are same then we are not able to call this such function
at same time. Because local variable will gets high priority over the function.
Q9: If address of x is 100 and then what is the output (In 16bit compiler)?
void main()
{
int x=10;
int y=20;
int z=30;
printf("%u %u %u",&x,&y,&z);
}
Options:
a. Cant determine
b. 100 98 96
c. 100 102 104
d. 100 104 108
Solution: This program is already discussed in the beginning of the variables.
Q10: If address of x is 100 and then what is the output(In 16bit compiler)?
25 | P a g e C L a n g u a g e M a t e r i a l b y G i r r a j S h a r m a
void main()
{
int x=10;
int y=20;
int z=30;
printf("%u %u",&x,&z);
}
Options:
a. Cant determine
b. 100 96
c. 100 104
d. 100 108
Q2: If address of x is 100 and then what is the output(In 16bit compiler)?
void main()
{
int x=10;
int y;
int z=30;
printf("%u %u",&x,&z);
y = 20;
}
Options:
a. 100 98
b. 100 96
c. 100 102
d. 100 104
Solution: In this case variable y used at the end of the program. So y also takes the memory space
between the x and z. Allocation of x,y,z is performed according to declaration order. So the address
difference of x and z is 4. Because according to stack representation if the address of x is 100 then
address of y is 98 and address of z is 96.
int x=10;
{
int y=20;
printf("%d %d ",x,y);
}
{
int z;
printf("%d %d",x,z);
}
}
Options:
a. 10 20 10 20
b. 10 20 10 garbage
c. Error
d. Multiple blocks not allowed
Solution: Already discussed somewhere in the beginning of this unit.
Solution: this code will not compile because variable x is declared after the printf() calling which is
executable statement. And as we know after executable statement variable declaration is not allowed.