0% found this document useful (0 votes)
2 views28 pages

2 Variable

The document provides a comprehensive overview of variables in C programming, covering their definitions, properties, types, and rules for declaration and usage. It explains the differences between local and global variables, as well as primitive and pointer variables, and includes examples of various syntaxes and statements. Additionally, it discusses memory allocation for local variables and the scope of variables within functions and blocks.

Uploaded by

Utkarsh Gole
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views28 pages

2 Variable

The document provides a comprehensive overview of variables in C programming, covering their definitions, properties, types, and rules for declaration and usage. It explains the differences between local and global variables, as well as primitive and pointer variables, and includes examples of various syntaxes and statements. Additionally, it discusses memory allocation for local variables and the scope of variables within functions and blocks.

Uploaded by

Utkarsh Gole
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1|Page C Language Mat erial by Girraj Sharma

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

Assume value of x is 3 and value of y is 4


2x – 3y + 5
Assume value of x is 5 and value of y is 3 …etc
2x – 3y + 5
As we seen in this expression we can put the any value of x and y in order to solve this expression
because x and y are the variables. Same kind of mathematical expression can be used in the
program.
Variable is a most common thing in the program that is used for storing the data.
Data is nothing but it is kind of value that are used by the program.

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.

Various syntaxes of the variables:


Syntax1:
storageclass datatype variable = value;
Syntax2:
datatype variable = value;
Syntax3:
storageclass variable = value;
Syntax4:
datatype variable;
Note: We will discuss about the datatype and storage class later in this course.

Ex: int x = 10;


In this example x is a variable name and int is the datatype of x, and 10 is the value of variable x,
x = 10 means 10 is stored inside the memory that are having the logical/variable name x.
Types of statements:
Based on different-2 processing phase of C program all statements are categories into three types.
1. Declarative statement
2. Executable statement
3. Pre-processing statement
Declarative statement:
 Any statement in c program which is starts from datatype or storage classes known as
declarative statement.
 Usually declarative statements are compile time information to the compiler or programmer.
 All variable declaration, function declaration, structure declaration, union declaration, enum
declarations are the part of declarative statements.
3|Page C Language Mat erial by Girraj Sharma

Imp tips: Declaration always starts with datatype or storage class


Some examples of declarative statements are:
int x;
static x;
unsigned x = 10;
long int x = 20;
register char x = 'A';
const int x=10;
int m1();
int m1(int,int);
struct student{int sno,int fee};
union student{int sno,int fee};
enum Color{RED,GREEN,BLUE};
Executable statement:
Any statement which doesn’t starts with datatype or storage classes known as executable statements
Ex: x = 10;
a = 10 + 20
fun();
clrscr();
printf("Hello");
Pre-processing statements:
Any statement which is starts with # symbol known as pre-processing statements.
#include
#define
More about semicolon:
 Semicolon ; is a valid statement in c
 Inside any function semicolon acts like a executable statement
 Outside any function semicolon acts like a declarative statement
void main()
{
; //executable statement
}
; //declarative statement
Things to be know about the variables
 All variables are taking memory space in the RAM. The size taken by any variable depends upon
the datatype of the variable.
 Most commonly used datatypes are char, int, float,double.
 The size of char type variables are always 1byte.
 The size of int type variables may worry compiler to compiler.
 There are three types of C compilers are available
1. 16bit c compiler
2. 32bit c compiler
3. 64bit c compiler
 In 16bit C compiler size of int type variables are 2bytes
 In 32bit an 64 bit C compiler size of int type variables are 4bytes
Note: We will discuss in depth about the database later in this course. If you are not able to cover
the property about the datatype just ignore the datatypes in this time.
 The size of float is 4 bytes and the size of double is 8 bytes.
4|Page C Language Mat erial by Girraj Sharma

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 representation of 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.

 We can display address of any variable by using & (address at operator)

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.

Rules of local variables:


 Before using any local variable we must provide its declaration.
Ex: void main()
{
int x = 10;
printf("%d ",x); //valid
}
Output: 10
Ex: void main()
{
printf("%d ",x); //invalid
int x = 10;
}
Output: Error
In this example we are using the x inside printf() before providing the declaration of x that’s why
this program will not be compiled.

 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

printf("%d ",y); //invalid


{
printf(" %d",x);
printf(" %d",y); //Invalid
}
}
Output: Compiletime error
Explanation: Access permission of y is not allowed from outside block in which y is declared.

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
}

Output:- compiletime error: undefined symbol y


Explanation: In C language we can’t access any variable before its declaration, any variable can
be accessed only after the declaration. As we seen in this program y is accessed inside main()
function where the declaration of y is provided after the main() function. But it is valid access of
y inside fun() function because before the fun() function y is already got declared.
 Default value of all global variables is 0
Ex:
int x,y;
int z;
void main()
{
printf("%d %d %d",x,y,z);
}
Output: 0 0 0
 Global variables can’t be initialized by another variable or by using dynamic expression.
Ex:
int x = 10; //valid
int y = x; //invalid
void main()
{
int a = 10; //valid
int b = a; //valid
}
Output: Compiletime error
Explanation: This code will not compile because y is initialized by x. And global variables cant
be initialized by using another variable.
 Global variables are persists in the memory till the end of execution.
 Local and global variables can have same names. If local and global variables having same names
then local variables will gets the high priority over the global variables.
13 | 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

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.

Scope Resolution Operator (::) operator


Ans: If inside any function any local variable is having with the same name of global variables then
by default we are not able to access the global variables in this function body. In such cases we can
access the global variables explicitly by using SRO (::) operator.
Syntax: ::global_variable

Q: What is the difference between local variable and global variable?


No Local variable Global variable
1 Declared inside function or block Declared outside the function
2 Memory allocation is performed inside stack Allocation is performed inside data area
area
3 Default value is garbage Default value is zero
4 Can be accessed only inside the current Can be accessed by the any function
function
5 Default storage class is auto Default storage class is extern
6 Memory is allocated whenever execution Memory is allocated at the beginning of
control comes inside the block execution of program
7 Memory allocation of local variables can be Memory is allocated only once.
happening multiple times.
14 | 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

8 Supports dynamic initialization Doesn’t supports dynamic initialization

Q: Whenever we should go for local variable?


Ans: If you need to provide the access permission of any variable only inside a single function only,
in such cases it is recommended to use local variables.
Q: Whenever we should go for global variable?
Ans: If you need to provide the access permission of any variable inside the body of more than one
function , in such cases it is recommended to use global variables.

Naming rules of variables:


 Variable name can contain only uppercase and lowercase alphabets, digits and underscore.
Valid name: age, PI, salary, marks1, marks2, account_no, __START__, fee, __, abc123, _fee,
abc25id
Invalid name: cash$amount, abc@pqr, account no,
 Variable name can't starts with digits.
Valid name: marks1, marks2, student1, c99type, student2, batch2, batch3, section1
Invalid name: 1marks, 2marks,
 Variables are case sensitive.
Valid name: coursefee, COURSEFEE, courseFee, CourseFee (all are unique variable name)
 Keywords are not allowed as variable name.
Invalid name: case, while, default, return, short, register, volatile
 Variables can be started with underscore
Valid name: __fee, __, std__, __fee__
 Length of variable name can be upto 32 character wide.
 If we are trying to use more than 32 characters in the variable name then compiler doesn’t reports
any error. In such cases after 32 characters remaining characters are discarded/ignored by the
compiler
Ex: void main()
{
int aaaaaaaabbbbbbbbccccccccddddddddxyz=10;
printf("%d",aaaaaaaabbbbbbbbccccccccdddddddd123); //valid
}
Output: 10
Explanation: This program is valid even though variable name is matching. In this case
compiler is matching only first 32 characters of the variable name which is same, compiler has
discarded extra length.

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

d. Can start with underscore

Answer - Practice1: variables


1 C 6 C
2 A 7 B
3 C 8 B
4 D 9 B
5 C 10 C

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

Answer – Practice2: variables


1 C, D 6 C
2 B 7 D
3 D 8 C
4 D 9 B
5 B 10 B

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

Answer – Practice3: variables


1 A 6 D
2 B 7 C
3 A 8 B
21 | 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

4 B 9 D
5 B 10 A

Solution:- 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
Solution: Option c is the incorrect answer because local variable declaration is not allowed outside the
function, local variables can be declared inside the block, inside function and header of the function as
a parameter.

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
Solution: Inside data area only global variables are created and method area used to store the compile
code. Local variables are created only inside the stack and sometimes in cpu register memory.

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
Solution: It is optional to initialize the local variable, and the default value is garbage not zero. We
can also change the value of local variables. So the correct option is c.

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
Solution: c and d are correct option. Because if local variable having register storage class then it will
be created inside cpu register memory otherwise local variables are created inside stack.

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
Solution: Option c is correct because global variable declaration is allowed only outside the function.

Q6: Which is true about global variables?


a. Created inside stack
b. Created inside register
c. Created inside data area
22 | 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

d. Created inside method area


Solution: option c is correct because global variables always creates inside data area only.

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
Solution: option b is correct because default value of global variable is zero. It is optional to initialize
the global variables, and we can also change the value of global variables.

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
Solution: Option b is correct because global variables are created inside the data area at the time of
code loading or beginning of the execution of program.

Q9: We must initialize global variables?


a. true
b. false
Solution: Option b is correct because it is optional to initialize the global variables.

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
d. Can start with underscore
Solution: Option c is correct because variable names cant start with digits.

Solution:- Practice2: variables


Q1: Which is an invalid identifier names?
a. __
b. __xyz__
c. 1s
d. student#marks
Solution: Option c and d are incorrect because identifier or variable names cant start with a digits and
doesn’t allowing special character (#).

Q2: Which is invalid identifier names?


a. m1
b. 1m
c. _1
d. _Hello25
Solution: option b is incorrect because identifier cant starts with digits.

Q3: What is the output?


23 | 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 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.

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
Solution: Keywords cant be allow to use as a variable name so the variable ‘do’ is an invalid variable
that’s why this code will not compile.

Q5: What is the output?


void main()
{
int x=10;
float x = 2.5;
printf("%f",x);
}
Options:
a. 10
b. Error
c. 2.5
d. 2.500000
Solution: As we know we cant declare multiple variables with the same names within a block or
function. Code will not compiled because variable x is declared two times.

Q6: What is the maximum length of an identifier name?


a. 8
b. 16
c. 32
d. No limit
Solution: No need to explain read the variable rules carefully.

Q7: What is the output?


24 | 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 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.

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
Solution: expressions are executable statements. And the executable statements are not allowed
outside the function. In C language we can place the executable statements only inside the body of
function. Outside the function only declarative statements are allowed. And the expression x = x*2; is
placed directly outside function so the program will not compiled.

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

Solution:- 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
Solution: This program is already discussed in this course so you need to revision.

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.

Q3: What is the output if program is executed based on stack property?


void main()
{
26 | 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
Solution: Already discussed somewhere in the beginning of this unit.

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.
Solution: Option b is incorrect about the scope.
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
Solution: Option b is incorrect because local variable declaration is not allowed after the executable
statement in the function. Local declarations are allowed only top of the block.

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
27 | 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

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.

Q7: What is the output?


void main()
{
int x = 10;
{
int x = 20;
printf("%d ",x);
x = 5;
}
printf("%d ",x);
}
Options:
a. Error
b. 20 5
c. 20 10
d. 20 20
Solution: No need to discus already covered in this unity.

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
Solution: If local variable and global variables having same names then local variable will gets high
priority over the global variable.

Q9: What is the output?


int x = 1;
void main()
{
int x = 2;
printf("%d %d",x,::x);
}
Options:
a. 11
b. 22
c. 12
d. 21
Solution: Already discussed.
28 | 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

Q10: What is the output?


void main()
{
int x = 1;
{
int x = 2;
printf("%d %d",x,::x);
}
}
Options:
a. Error
b. 22
c. 12
d. 21
Solution: This code will not compiled because of ::x, scope resolution operator directly checking the
definition of x outside the main() function in global scope. And in this program there is no global
variable with the name x.

You might also like