SlideShare a Scribd company logo
1 of 4
Storage Class:
'Storage' refers to the scope of a variable and memory allocated by compiler to store
that variable. Scope of a variable is the boundary within which a variable can be used.
Storage class defines the scope and lifetime of a variable.
From the point view of C compiler, a variable name identifies physical location from
a computer where variable is stored. There are two memory locations in a computer system
where variables are stored as : Memory and CPU Registers.
Functions of storage class :
To detemine the location of a variable where it is stored ?
Set initial value of a variable or if not specified then setting it to default value.
Defining scope of a variable.
To determine the life of a variable.
Types of Storage Classes :
Storage classes are categorised in 4 (four) types as,
 Automatic Storage Class
 Register Storage Class
 Static Storage Class
 External Storage Class
Automatic Storage Class :
oKeyword : auto
oStorage Location : Main memory
oInitial Value : Garbage Value
oLife : Control remains in a block where it is defined.
oScope : Local to the block in which variable is declared.
Syntax :
auto [data_type] [variable_name];
Example :
auto int a;
Program :
/* Program to demonstrate automatic storage class.
#include <stdio.h>
#include <conio.h>
void main()
{
auto int i=10; Output
clrscr(); 20
{ 10
auto int i=20;
printf("nt %d",i);
}
printf("nnt %d",i);
getch();
}
Example 2
int main()
{ int m=1000;
function2();
printf(“%dn”,m);
}
function1()
{
int m = 10;
printf(“%dn”,m);
}
function2()
{ int m = 100;
function1();
printf(“%dn”,m);
}
Output
10
100
1000
Register
o Keyword : register
o Storage Location : CPU Register
o Initial Value : Garbage
o Life : Local to the block in which variable is declared.
o Scope : Local to the block.
Syntax :
register [data_type] [variable_name];
Example :
register int a;
When the calculations are done in CPU, then the value of variables are transferred
from main memory to CPU. Calculations are done and the final result is sent back to main
memory. This leads to slowing down of processes.
Register variables occur in CPU and value of that register variable is stored in a
register within that CPU. Thus, it increases the resultant speed of operations. There is no
waste of time, getting variables from memory and sending it to back again.
It is not applicable for arrays, structures or pointers.
It cannot not used with static or external storage class.
Unary and address of (&) cannot be used with these variables as explicitly or implicitly.
Program :
/* Program to demonstrate register storage class.
#include <stdio.h>
#include <conio.h>
void main()
{
register int i=10;
clrscr();
{
register int i=20;
printf("nt %d",i);
}
printf("nnt %d",i);
getch();
}
Output
20
10
Static Storage Class :
o Keyword : static
o Storage Location : Main memory
o Initial Value : Zero and can be initialize once only.
o Life : depends on function calls and the whole application or program.
o Scope : Local to the block.
Syntax :
static [data_type] [variable_name];
Example :
static int a;
There are two types of static variables as :
a) Local Static Variable
b) Global Static Variable
Static storage class can be used only if we want the value of a variable to persist between
different function calls.
Program :
/* Program to demonstrate static storage class.
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
void incre(void);
clrscr();
for (i=0; i<3; i++)
incre();
getch();
}
void incre(void)
{
int avar=1;
static int svar=1;
avar++;
svar++;
printf("nn Automatic variable value : %d",avar);
printf("t Static variable value : %d",svar);
}
Output :
Automatic variable value : 2 Static Variable Value :2
Automatic variable value : 2 Static Variable Value :3
Automatic variable value : 2 Static Variable Value :4
Internal static variable
• Are those which are declared inside a function.
• Scope of Internal static variables extend upto the end of the program in which they
are defined.
• Internal static variables are almost same as auto variable except they remain in
existence (alive) throughout the remainder of the program.
• Internal static variables can be used to retain values between function calls.
External static variables
• An external static variable is declared outside of all functions and is available to all
the functions in the program.
• An external static variable seems similar simple external variable but their difference
is that static external variable is available only within the file where it is defined while
simple external variable can be accessed by other files.
Static function
Static declaration can also be used to control the scope of a function.
• If you want a particular function to be accessible only to the functions in the file in
which it is defined and not to any function in other files, declare the function to be
static. eg.
static int power(int x inty)
{
. . .
. . .
}
External Storage Class :
o Keyword : extern
o Storage Location : Main memory
o Initial Value : Zero
o Life : Until the program ends.
o Scope : Global to the program.
Syntax :
extern [data_type] [variable_name];
Example :
extern int a;
The variable access time is very fast as compared to other storage classes. But few registers
are available for user programs.
The variables of this class can be referred to as 'global or external variables.' They are
declared outside the functions and can be invoked at anywhere in a program.
• Don’t try to declare a global variable as register. Because the register will be occupied
during the lifetime of the program.
Program :
/* Program to demonstrate external storage class.
#include <stdio.h>
#include <conio.h>
extern int i=10;
void main()
{
int i=20;
void show(void);
clrscr();
printf("nt %d",i); Output
show(); 20
getch(); 10
}
void show(void)
{
printf("nnt %d",i);
}

More Related Content

What's hot

storage class
storage classstorage class
storage classstudent
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++Jaspal Singh
 
What is storage class
What is storage classWhat is storage class
What is storage classIsha Aggarwal
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and FunctionsJake Bond
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c languagetanmaymodi4
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageJenish Bhavsar
 
JavaScript Execution Context
JavaScript Execution ContextJavaScript Execution Context
JavaScript Execution ContextJuan Medina
 
Presentation mcrl2
Presentation mcrl2Presentation mcrl2
Presentation mcrl2matifch
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class웅식 전
 
Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Adam Mukharil Bachtiar
 

What's hot (20)

storage class
storage classstorage class
storage class
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
 
Storage class
Storage classStorage class
Storage class
 
What is storage class
What is storage classWhat is storage class
What is storage class
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage classes
Storage classesStorage classes
Storage classes
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Storage class
Storage classStorage class
Storage class
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C Language
 
JavaScript Execution Context
JavaScript Execution ContextJavaScript Execution Context
JavaScript Execution Context
 
Presentation mcrl2
Presentation mcrl2Presentation mcrl2
Presentation mcrl2
 
Loops in R
Loops in RLoops in R
Loops in R
 
11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class11 2. variable-scope rule,-storage_class
11 2. variable-scope rule,-storage_class
 
Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)
 

Similar to Storage Classes in C: Scope, Lifetime & Location

Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxCheriviralaNikhil
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and AnswersDaisyWatson5
 
Advanced C Programming Notes
Advanced C Programming NotesAdvanced C Programming Notes
Advanced C Programming NotesLeslie Schulte
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c languageTanmay Modi
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, RecursionSreedhar Chowdam
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1karmuhtam
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variablesSaurav Kumar
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semKavita Dagar
 
Ch10 Program Organization
Ch10 Program OrganizationCh10 Program Organization
Ch10 Program OrganizationSzeChingChen
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 FunctionDeepak Singh
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptSandipPradhan23
 
C Storage Classes VSN
C Storage Classes VSNC Storage Classes VSN
C Storage Classes VSNNITTTR
 

Similar to Storage Classes in C: Scope, Lifetime & Location (20)

Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptx
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
Advanced C Programming Notes
Advanced C Programming NotesAdvanced C Programming Notes
Advanced C Programming Notes
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
visiblity and scope.pptx
visiblity and scope.pptxvisiblity and scope.pptx
visiblity and scope.pptx
 
5.program structure
5.program structure5.program structure
5.program structure
 
STORAGE CLASS.pptx
STORAGE CLASS.pptxSTORAGE CLASS.pptx
STORAGE CLASS.pptx
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-sem
 
C language updated
C language updatedC language updated
C language updated
 
Ch10 Program Organization
Ch10 Program OrganizationCh10 Program Organization
Ch10 Program Organization
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
C Storage Classes VSN
C Storage Classes VSNC Storage Classes VSN
C Storage Classes VSN
 

Recently uploaded

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 

Recently uploaded (20)

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 

Storage Classes in C: Scope, Lifetime & Location

  • 1. Storage Class: 'Storage' refers to the scope of a variable and memory allocated by compiler to store that variable. Scope of a variable is the boundary within which a variable can be used. Storage class defines the scope and lifetime of a variable. From the point view of C compiler, a variable name identifies physical location from a computer where variable is stored. There are two memory locations in a computer system where variables are stored as : Memory and CPU Registers. Functions of storage class : To detemine the location of a variable where it is stored ? Set initial value of a variable or if not specified then setting it to default value. Defining scope of a variable. To determine the life of a variable. Types of Storage Classes : Storage classes are categorised in 4 (four) types as,  Automatic Storage Class  Register Storage Class  Static Storage Class  External Storage Class Automatic Storage Class : oKeyword : auto oStorage Location : Main memory oInitial Value : Garbage Value oLife : Control remains in a block where it is defined. oScope : Local to the block in which variable is declared. Syntax : auto [data_type] [variable_name]; Example : auto int a; Program : /* Program to demonstrate automatic storage class. #include <stdio.h> #include <conio.h> void main() { auto int i=10; Output clrscr(); 20 { 10 auto int i=20; printf("nt %d",i); } printf("nnt %d",i); getch(); } Example 2 int main() { int m=1000; function2(); printf(“%dn”,m); }
  • 2. function1() { int m = 10; printf(“%dn”,m); } function2() { int m = 100; function1(); printf(“%dn”,m); } Output 10 100 1000 Register o Keyword : register o Storage Location : CPU Register o Initial Value : Garbage o Life : Local to the block in which variable is declared. o Scope : Local to the block. Syntax : register [data_type] [variable_name]; Example : register int a; When the calculations are done in CPU, then the value of variables are transferred from main memory to CPU. Calculations are done and the final result is sent back to main memory. This leads to slowing down of processes. Register variables occur in CPU and value of that register variable is stored in a register within that CPU. Thus, it increases the resultant speed of operations. There is no waste of time, getting variables from memory and sending it to back again. It is not applicable for arrays, structures or pointers. It cannot not used with static or external storage class. Unary and address of (&) cannot be used with these variables as explicitly or implicitly. Program : /* Program to demonstrate register storage class. #include <stdio.h> #include <conio.h> void main() { register int i=10; clrscr(); { register int i=20; printf("nt %d",i); } printf("nnt %d",i); getch(); } Output
  • 3. 20 10 Static Storage Class : o Keyword : static o Storage Location : Main memory o Initial Value : Zero and can be initialize once only. o Life : depends on function calls and the whole application or program. o Scope : Local to the block. Syntax : static [data_type] [variable_name]; Example : static int a; There are two types of static variables as : a) Local Static Variable b) Global Static Variable Static storage class can be used only if we want the value of a variable to persist between different function calls. Program : /* Program to demonstrate static storage class. #include <stdio.h> #include <conio.h> void main() { int i; void incre(void); clrscr(); for (i=0; i<3; i++) incre(); getch(); } void incre(void) { int avar=1; static int svar=1; avar++; svar++; printf("nn Automatic variable value : %d",avar); printf("t Static variable value : %d",svar); } Output : Automatic variable value : 2 Static Variable Value :2 Automatic variable value : 2 Static Variable Value :3 Automatic variable value : 2 Static Variable Value :4 Internal static variable • Are those which are declared inside a function. • Scope of Internal static variables extend upto the end of the program in which they are defined. • Internal static variables are almost same as auto variable except they remain in existence (alive) throughout the remainder of the program. • Internal static variables can be used to retain values between function calls.
  • 4. External static variables • An external static variable is declared outside of all functions and is available to all the functions in the program. • An external static variable seems similar simple external variable but their difference is that static external variable is available only within the file where it is defined while simple external variable can be accessed by other files. Static function Static declaration can also be used to control the scope of a function. • If you want a particular function to be accessible only to the functions in the file in which it is defined and not to any function in other files, declare the function to be static. eg. static int power(int x inty) { . . . . . . } External Storage Class : o Keyword : extern o Storage Location : Main memory o Initial Value : Zero o Life : Until the program ends. o Scope : Global to the program. Syntax : extern [data_type] [variable_name]; Example : extern int a; The variable access time is very fast as compared to other storage classes. But few registers are available for user programs. The variables of this class can be referred to as 'global or external variables.' They are declared outside the functions and can be invoked at anywhere in a program. • Don’t try to declare a global variable as register. Because the register will be occupied during the lifetime of the program. Program : /* Program to demonstrate external storage class. #include <stdio.h> #include <conio.h> extern int i=10; void main() { int i=20; void show(void); clrscr(); printf("nt %d",i); Output show(); 20 getch(); 10 } void show(void) { printf("nnt %d",i); }