You are on page 1of 35

CSE109

• Storage Classes and Scope Rules

©LPU CSE101 C Programming


Outline
• Storage Classes
– auto
– static
– extern
– register
• Scope Rules

©LPU CSE101 C Programming


Storage Classes
• Storage Classes are used to describe the features of a variable.
These features basically include the scope, visibility and
life-time which help us to trace the existence of a particular
variable during the runtime of a program.
• Storage class specifies four things-
I. Storage location: Where the variable will be stored?[ In
memory /or CPU register]
II. Scope: Block in which the variable is accessible.
III. Lifetime: How long would the variable exist?
IV. Default initial value: What will be the initial value of the
variable, if initial value is not specifically assigned ?

©LPU CSE101 C Programming


Storage Classes: Auto
• Automatic storage
– auto int x, y;
– It is the default storage class for a local variable
– This is the default storage class for all the variables
declared inside a function or a block
Storage − Memory(RAM).
Default initial value − An unpredictable value, which is
often called a garbage value.
Scope − Local to the block in which the variable is
defined.
Lifetime − Till the control remains within the block in
which the variable is defined.

©LPU CSE101 C Programming


Program example-auto storage class
#include<stdio.h>
void func1()
{
auto int a=10; // Local variable of func1()
printf("\n a=%d",a);
}
void func2()
{
auto int a=20; //Local variable of func2()
printf("\n a=%d",a);
}
int main()
{
auto int a=30;//Local variable of main()
func1();
func2();
printf("\n a=%d",a);
return 0;
}

©LPU CSE101 C Programming


Storage Classes: Register
• register: tries to put variable into
high-speed registers.
– register int counter = 1;

Storage - CPU registers.


Default initial value - Garbage value.
Scope - Local to the block in which the variable is
defined.
Lifetime - Till the control remains within the block in
which the variable is defined.

©LPU CSE101 C Programming


More points in relation to register storage class

• This storage class declares register variables which have the same
functionality as that of the auto variables. The only difference is that the
compiler tries to store these variables in the register of the microprocessor
if a free register is available.
• This makes the use of register variables to be much faster than that of the
variables stored in the memory during the runtime of the program. If a
free register is not available, these are then stored in the memory only.
• Usually few variables which are to be accessed very frequently in a
program are declared with the register keyword which improves the
running time of the program. An important and interesting point to be
noted here is that we cannot obtain the address of a register variable using
pointers.

©LPU CSE101 C Programming


Program example-register storage class
#include<stdio.h>
int main()
{
register int i; // i will be used frequently so, it can be given register storage class
for(i=1;i<=20;i++)
{
printf("\n%d",i);
}
return 0;
}

©LPU CSE101 C Programming


Storage Classes: Static
• Static storage
Storage − Memory(RAM).
Default initial value − Zero.
Scope − Local to the block in which the variable is
defined.
Life time − variable will retain its value throughout the
program

©LPU CSE101 C Programming


More points in relation to static storage class

• Static variables have a property of preserving their value even


after they are out of their scope! Hence, static variables
preserve the value of their last use in their scope.
• So we can say that they are initialized only once and exist till
the termination of the program. Thus, no new memory is
allocated because they are not re-declared.
• Their scope is local to the function to which they were
defined. Global static variables can be accessed anywhere in
the program. By default, they are assigned the value 0 by the
compiler.

©LPU CSE101 C Programming


Program example-static storage class
#include<stdio.h>
void function();
int main()
{
function();
Output:
function(); Value of a:10, Value of b:10
function(); Value of a:10, Value of b:11
return 0;
}
Value of a:10, Value of b:12
void function()
{
int a=10;
static int b=10;
printf("\n Value of a:%d, Value of b:%d",a,b);
a++;
b++;
}

©LPU CSE101 C Programming


Storage Classes: extern
Extern storage class simply tells us that the variable is
defined elsewhere and not within the same block
where it is used. Basically, the value is assigned to it in a
different block and this can be overwritten/changed in
a different block as well
Storage − Memory(RAM).
Default initial value − Zero.
Scope − Global.
Life − As long as the program’s execution doesn’t come to
an end.

©LPU CSE101 C Programming


More points in relation to extern storage
class
• extern variable is nothing but a global variable
initialized with a legal value where it is declared in
order to be used elsewhere. It can be accessed within
any function/block.
• Also, a normal global variable can be made extern as
well by placing the ‘extern’ keyword before its
declaration/definition in any function/block.
• This basically signifies that we are not initializing a new
variable but instead we are using/accessing the global
variable only. The main purpose of using extern
variables is that they can be accessed between two
different files which are part of a large program.

©LPU CSE101 C Programming


Program example 1-extern storage class
External variable in the same file
#include<stdio.h>
void first();
int main()
{
extern int x; /* declaration in main() */
printf("\nx=%d",x); // x is used before its definition[Possible because of extern]
first();
printf("\nx=%d",x);// Changes done by first are visible here
return 0;
}
void first()
{
extern int x; /* declaration in first() */
printf("\nx=%d",x); // x is used again before its definition[Possible because of extern]
x=x+10;
}
int x=10; /* definition of external variable, here x is global variable */

©LPU CSE101 C Programming


Program example 2-extern storage class
External variable in different file
extern1.c file
extern2.c file
#include<stdio.h>
void print()
#include"extern2.c" {
//Global variable declared in extern1.c extern int x;//Taking reference of global
int x=30; variable in different file or Declaration
int main() printf("%d\n",x);
{ x=x+10;
print(); }
printf("%d",x);//Changes done by extern2.c //Output:
file are also reflected 30
} 40

©LPU CSE101 C Programming


Summary

©LPU CSE101 C Programming


Scope Rules
• The scope of a variable is the portion of a
program where the variable has meaning
(where it exists).
• A global variable has global (unlimited) scope.
• A local variable’s scope is restricted to the
function that declares the variable.
• A block variable’s scope is restricted to the
block in which the variable is declared.

©LPU CSE101 C Programming


Local variables
• Parameters and variables declared inside the
definition of a function are local.
• They only exist inside the function body.
• Once the function returns, the variables no
longer exist!

©LPU CSE101 C Programming


Example-local variable
#include<stdio.h>
void function();
int main()
{
int a=1,b=2;
printf("\n a is:%d,b is:%d",a,b);//a,b are local variables of main()
function();
return 0;
}
void function()
{
int c=3;
printf("\n Value of c is:%d",c);// c is a local variable of function
}

©LPU CSE101 C Programming


Block Variables
• You can also declare variables that exist only
within the body of a compound statement (a
block):
{
int f;


}

©LPU CSE101 C Programming


Example-block scoped variable
#include<stdio.h>
int main()
{
int b=2;
{
int a=1; // a is block variable
printf("\nValue of a is:%d",a);
}
printf("\nValue of b is:%d",b);
return 0;
}

©LPU CSE101 C Programming


Global variables
• You can declare variables outside of any
function definition – these variables are
global variables.
• Any function can access/change global
variables.

©LPU CSE101 C Programming


Example-global variable
#include<stdio.h>
int a=1;// a is a global variable
void print();
int main()
{
printf("\nValue of a is:%d",a);
print();
return 0;
}
void print()
{
printf("\nValue of a is:%d",a);
}

©LPU CSE101 C Programming


More examples-Scope rules
#include<stdio.h>
int a=10;// a is a global variable
Output:
void print(); Value of a is:1
int main()
{ Value of a is:10
int a=1;
printf("\nValue of a is:%d",a);// It will
Note:
access local a When we have same named
print();
return 0; local and global variables,
} priority is always given to local
void print()
{
variable first, that is why in
printf("\nValue of a is:%d",a); //It will main() function value of local a is
access global a
}
printed, whereas in function
definition, there is no local
variable so preference is given to
global version of a
©LPU CSE101 C Programming
More examples-Scope rules
#include<stdio.h> Output:
Value of a is:10
int a=10;// a is a global variable
Value of a is:10
void print(); Value of a is:20
int main()
{
printf("\nValue of a is:%d",a);
print();
printf("\nValue of a is:%d",a);// Change
done by print to global a is reflected here
return 0;
}
void print()
{
printf("\nValue of a is:%d",a);
a=20;
}

©LPU CSE101 C Programming


More examples-Scope rules
#include<stdio.h> Output:
int main() a:500
{ a:50
int a=5; a:5
{
int a=50;
{
int a=500;
printf("\na:%d",a);
}
printf("\na:%d",a);
}
printf("\na:%d",a);
return 0;
}

©LPU CSE101 C Programming


Q1
What will be the output of the
following C code?
#include <stdio.h>
int x; A. 0
void m();
int main() B. 4
{ C. Compile time error
m();
printf("%d", x);
D. Runtime error
return 0;
}
void m()
{
x = 4;
}

©LPU CSE101 C Programming


Q2
What will be the output of the following C code?
#include <stdio.h> A. 8 3
int x = 5;
void m(); B. 3 8
void n();
int main()
C. 8 5
{ D. 5 3
int x = 3;
m();
printf("%d", x);
return 0;
}
void m()
{
x = 8;
n();
}
void n()
{
printf("%d", x);
}
©LPU CSE101 C Programming
Q3
What will be the output of following
code?
#include <stdio.h> A. 1
int main()
{ B. 2
int x=1;
{
C. 3
x=2; D. Compile time error
{
int x=3;
}
}
printf("%d",x);
return 0;
}

©LPU CSE101 C Programming


Q4
In case of a conflict between the names of a
local and global variable what happens?
A. The global variable is given a priority.
B. The local variable is given a priority.
C. Which one will get a priority depends upon
which one is defined first.
D. The compiler reports an error.

©LPU CSE101 C Programming


Q5
What will be the storage class of variable i in the code
written below?
#include<stdio.h>
int main()
{
int i = 10;
printf("%d",i);
return 0;
}
A. Automatic storage class
B. Extern storage class
C. Static storage class
D. Register storage class

©LPU CSE101 C Programming


Q6
What will be the behaviour of following code?
#include<stdio.h>
int main()
{
register int a;
printf("\nEnter value of a:");
scanf("%d",&a);
return 0;
}
A. Program will work normally
B. Compile time error
C. Runtime error
D. None of the above

©LPU CSE101 C Programming


Q7
#include<stdio.h> A. 10
int incr(int i)
{ B. 4
static int count = 0; C. 3
count = count + i;
return (count); D. 2
}
int main()
{
int i,j;
for (i = 0; i <=2; i++)
j = incr(i);
printf("%d",j);
return 0;
}

©LPU CSE101 C Programming


Q8
#include<stdio.h> A. 2,2
void update();
int main() 2,3
{ B. 2,2
update();
update(); 2,2
return 0; C. 1,1
}
void update() 1,2
{ D. 2,1
auto int a=1;
static int b=1; 2,2
a++;
b++;
printf("%d,%d\n",a,b);
}

©LPU CSE101 C Programming


Q9
#include<stdio.h>
int main()
{
extern int a;
printf("%d",++a);
return 0;
}
int a;

A. 0
B. 1
C. -1
D. Compile time error

©LPU CSE101 C Programming

You might also like