BCSE102L C Storage Classes
BCSE102L C Storage Classes
• Global variable
• declared very early stage
• available at all times from anywhere
• created at the start of the program, and lasts until the end, it is taking up lots
of memory
• difficult to debug
Methods of variable creation (continued)
int main()
{
int x; //local variable
for (x = 1; x <= 10; x++)
printf(“%d “, square(x));
return 0;
}
int square (int y) // Function definition
{ Local variable
return y * y;
}
Storage duration
• Period during which an identifier exists in memory.
• Some identifiers exists briefly.
• Some are repeatedly created and destroyed.
• Others exist for the entire execution of a program.
• Storage duration can be of two types:
– automatic (auto and register)
– static
Global and Local declarations
#include <iostream>
void func( float );
const int a = 17; // global constant
int b; // global variable
int c; // global variable
int main()
{
b = 4; // assignment to global b
c = 6; // assignment to global c
func(42.8);
return 0;
}
Output:
void func( float c) // prevents access to global c a = 17 b = 2.3 c = 42.8
{
float b; // prevent access to global b
b = 2.3; // assignment to local b
printf(“a= %d”, a ); // output global a (17)
printf(“b= %d”, b ); // output local b (2.3
printf(“c= %d”, c ); // output local c (42.8)
}
Explanation
• In this example, function func accesses global constant a.
• However, func declares its own local variable b and parameter c.
• Local variable b takes precedence over global variable b, effectively
hiding global variable b from the statements in function func.
• Function parameter acts like local variable.
Life time of a variable
void fun(int a)
{ int t;
t=a*4;
}
Storage Classes
• C provides four storage classes:
– auto
– static
– extern
– register
• An identifier’s storage class helps to determine its
storage duration and scope.
Auto Storage class
• Formal parameters and local variables of functions are variables
that are automatically allocated on the stack when a function is
called and automatically deallocated when the function returns.
• They are of storage class auto.
Static
• Static variable is allocated and initialized one time, prior to
program execution.
• It remains allocated until the entire program terminates.
Static Variables
#include <stdio.h>
• As soon as the function completes its int main()
execution, the memory of the count variable {
1
will be removed. printf("%d",func()); 1
• If we do not want to remove the count from printf("\n%d",func()); 1
memory, then we need to use the count printf("\n%d",func());
variable as static. return 0;
• If we declare the variable as static, then the }
variable will not be removed from the int func()
memory even when the function completes
its execution. {
int count=0; // variable initialization
count++; // incrementing counter variable
return count; }
Static Variables
#include <stdio.h>
int main()
{
printf("%d",func());
printf("\n%d",func());
printf("\n%d",func()); 1
2
return 0; 3
}
int func()
{
static int count=0; // variable initialization
count++; // incrementing counter variable
return count; }
Output?
#include <stdio.h>
void main()
{
m();
m();
}
void m()
{
static int x = 5;
x++;
printf("%d ", x);
}
Output?
#include <stdio.h>
67
void main()
{
m();
m();
}
void m()
{
static int x = 5;
x++;
printf("%d ", x);
}
Extern
• Storage class of names known to the linker.
• Example:
extern int square (int x);
• Means the function will be available to the linker.
• It notifies the compiler that such a function exists and
that the linker will know where to find it.
Register
• If you declare a variable of type register, it simply alerts the
compiler to the fact that this memory cell will be referenced more
often than most.
• Register is a special high-speed memory location inside the central
processor.
#include <stdio.h>