0% found this document useful (0 votes)
66 views23 pages

BCSE102L C Storage Classes

The document discusses different types of variables in C programming including global variables, local variables, and static variables. It explains that global variables are declared at the start and available everywhere but use more memory, while local variables are only available within the routine they are created in and use less memory. Static variables are initialized once and remain in memory for the whole program. The document also covers variable scope, storage duration, and different storage classes like auto, static, extern, and register.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views23 pages

BCSE102L C Storage Classes

The document discusses different types of variables in C programming including global variables, local variables, and static variables. It explains that global variables are declared at the start and available everywhere but use more memory, while local variables are only available within the routine they are created in and use less memory. Static variables are initialized once and remain in memory for the whole program. The document also covers variable scope, storage duration, and different storage classes like auto, static, extern, and register.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

BCSE102L STRUCTURED

AND OBJECT ORIENTED


PROGRAMMING
Storage Classes
Dr. R. Jothi SCOPE
Global Vs. Local Variables
Methods of variable creation

• 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)

• Local on the fly variables


• simply created when they are needed,
• only available from within the routine in which they were created
• memory requirement is less
• easy to debug
Methods of variable creation (continued)

• Local defined variable


• created before they are needed
• only available in the routine in which they were created.
• Memory requirement is less
• easy to debug
• the most usually favored method
Scope
• The scope of an identifier is the portion of the program in
which the identifier can be referenced.
• Some identifiers can be referenced throughout the
program.
• Others can be referenced from only portions of a program.
• Example:
When we declare a local variable in a block, it can be
referenced only in that block or in blocks nested within that
block.
A program written in C

1 // This program calculates the number calories in a cheese sandwich


2 #include <stdio.h>
3 const int BREAD = 63; // global constant
4 const int CHEESE = 106; // global constant
5 const int MAYONNAISE = 49; // global constant
6 const int PICKLES = 25; // global constant
7 int main()
8 {
9 int totalCalories; // local variable
10 totalCalories = 2 * BREAD + CHEESE + MAYONNAISE + PICKLES;
11 printf(“There were %d calories in my lunch yesterday.”, totalCalories);
12 return 0;
13 }
Function definition and function prototype

// A programmer-defined square function


#include <stdio.h>

int square (int); // Function prototype

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

• Local variables: variables declared inside a function or


variables declared as function parameter.
• When you will call the function, memory will be allocated
for all local variables defined inside the function.
• Finally memory will be deallocated when the function
exits.
• The period of time a variable will be “alive” while the
function is executing is called “lifetime” of this variable.
Example i is visible from this point to end
of fcn

int fcn (float a, int b) {


int i;
g is visible from this point
double g; to end of fcn

for (i = 0; i < b; i++) { h is only visible from this


double h = i*g; point to end of loop!
//loop body – may access a, b, i, g, h
}

fcn body – may access a, b, i, g


}
13
Memory Layout of a C program
int g=4;
void main()
{
x=3;
fun(2);
}

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>

int main() main.c:6:5: error: address of register variable ‘i’ requested


{ 6 | printf("%p", &i);
register int i = 0; | ^~~
printf("%p", &i);
return 0;
}

You might also like