0% found this document useful (0 votes)
20 views16 pages

Storage Classes

storage class

Uploaded by

punny
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)
20 views16 pages

Storage Classes

storage class

Uploaded by

punny
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/ 16

Storage Classes

• Storage Classes are used to describe the


features of a variable/function.
• 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.
Automatic variables
• Automatic variables are allocated memory automatically at
runtime.
• The visibility of the automatic variables is limited to the
block in which they are defined.
• The scope of the automatic variables is limited to the block
in which they are defined.The automatic variables are
initialized to garbage by default.
• The memory assigned to automatic variables gets freed
upon exiting from the block.
• The keyword used for defining automatic variables is auto.
• Every local variable is automatic in C by default.
#include <stdio.h>
int main()
{
int a; //auto
char b;
float c;
printf("%d %c %f",a,b,c); // printing initial default value o
f automatic variables a, b, and c.
return 0;
}
Output
garbage garbage grabage
Static Variables
• The variables defined as static specifier can hold their
value between the multiple function calls.
• Static local variables are visible only to the function or
the block in which they are defined.
• A same static variable can be declared many times but
can be assigned at only one time.
• Default initial value of the static integral variable is 0
otherwise null.
• The visibility of the static global variable is limited to
the file in which it has declared.
• The keyword used to define static variable is static.
#include<stdio.h>
void sum()
{
static int a = 10;
static int b = 24;
printf("%d %d \n",a,b);
a++;
b++;
}
void main()
{
int i;
for(i = 0; i< 3; i++)
{
sum(); // The static variables holds their value between multiple function calls.
}
}
Output
10 24
11 25
12 26
Register
• The variables defined as the register is allocated the memory into the CPU
registers depending upon the size of the memory remaining in the CPU.
• We can not dereference the register variables, i.e., we can not use &operator
for the register variable.
• The access time of the register variables is faster than the automatic
variables.
• The initial default value of the register local variables is 0.
• The register keyword is used for the variable which should be stored in the
CPU register. However, it is compiler?s choice whether or not; the variables
can be stored in the register.
• We can store pointers into the register, i.e., a register can store the address
of a variable.
• Static variables can not be stored into the register since we can not use more
than one storage specifier for the same variable.
#include <stdio.h>
int main()
{
register int a; // variable a is allocated memory in
the CPU register. The initial default value of a is
0.
printf("%d",a);
}
Output
0
#include <stdio.h>
int main()
{
register int a = 0;
printf("%u",&a); // This will give a compile time error
since we can not access the address of a register va
riable.
}
Error
main.c:5:5: error: address of register variable ?a?
requested printf("%u",&a);
External
• The external storage class is used to tell the compiler that the variable
defined as extern is declared with an external linkage elsewhere in the
program.
• The variables declared as extern are not allocated any memory. It is only
declaration and intended to specify that the variable is declared
elsewhere in the program.
• The default initial value of external integral type is 0 otherwise null.
• We can only initialize the extern variable globally, i.e., we can not
initialize the external variable within any block or method.
• An external variable can be declared many times but can be initialized at
only once.
• If a variable is declared as external then the compiler searches for that
variable to be initialized somewhere in the program which may be extern
or static. If it is not, then the compiler will show an error.
#include <stdio.h>
int a;
int main()
{
extern int a; // variable a is defined globally, the memory
will not be allocated to a
printf("%d",a);
}
Output
0
Memory Allocation
Methods for Dynamic Memory Allocation

• The concept of dynamic memory allocation in c


language enables the C programmer to allocate
memory at runtime. Dynamic memory
allocation in c language is possible by 4
functions of stdlib.h header file.
 malloc()
 calloc()
 realloc()
 free()
• Thanks

You might also like