0% found this document useful (0 votes)
60 views11 pages

Storage Classes in C: by Pundreekaksha Sharma Assistant Professor

Uploaded by

Akshay Arebelly
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)
60 views11 pages

Storage Classes in C: by Pundreekaksha Sharma Assistant Professor

Uploaded by

Akshay Arebelly
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/ 11

Storage Classes in C

By
Pundreekaksha Sharma
Assistant Professor

©LPU CSE101 C Programming


• Storage classes in C are used to determine the
lifetime, visibility, memory location, scope and
initial value of a variable. There are four types
of storage classes in C
• Auto
• Extern
• Static
• Register

©LPU CSE101 C Programming


Storage Storage Place Default Value Scope Lifetime
Classes
auto RAM Garbage Value Local Within function

extern RAM Zero Global Till the termination of the


program Maybe declared
anywhere in the program

static RAM Zero Local Till the termination of the


program, Retains value
between multiple functions call

register Register Garbage Value Local Within the function

©LPU CSE101 C Programming


Automatic
• 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.
• Syntax:
auto int a; or int a;

©LPU CSE101 C Programming


#include<stdio.h>
void fun(){
auto int a=20;
++a;
printf("%d\n",a);
}

void main(){
fun();
fun();
fun();
}

©LPU CSE101 C Programming


Static

• 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.
• 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.
• Syntax:
static int a;

©LPU CSE101 C Programming


#include<stdio.h>
void fun(){
static int a=20;
++a;
printf("%d\n",a);
}

void main(){
fun();
fun();
fun();
}

©LPU CSE101 C Programming


• 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.
• 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 compilers
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.

©LPU CSE101 C Programming


#include<stdio.h>
void main(){
register int x, sum=0;
for(x=1; x<10000; x++){
sum +=x;
}
printf("sum = %d",sum);
}

©LPU CSE101 C Programming


Extern:
• It is available through out the program they are declare outside the block.
• The default initial value of external integral type is 0.
• We can only initialize the extern variable globally, i.e., we can not initialize
the external variable within any block or method.
• A variable or function can be declared any number of times, but it can be
defined 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.

©LPU CSE101 C Programming


#include<stdio.h>
void fun1();
void main(){
extern int x;
printf("x = %d\n",x);
fun1();

}
fun1(){
int x=1;
x+=5;
printf("x = %d\n",x);
}
int x =20;

©LPU CSE101 C Programming

You might also like