C Storage Classes
Storage Class of Variables
• It refers to the permanence of a variable, and its
scope within a program.
• Four storage class specifications in C
• Automatic: auto
• External: extern
• Static: static
• Register: register
Automatic Variables
• These are always declared within a function and are
local to the function in which they are declared.
• Scope is confined to that function.
• This is the default storage class specification.
• All variables are considered as auto unless explicitly
specified otherwise.
• The keyword auto is optional.
• An automatic variable does not retain its value once control
is transferred out of its defining function.
Example: Auto
#include <stdio.h>
int factorial(int m)
{
auto int i;
auto int temp = 1;
for (i=1; i<=m; i++)
temp = temp * i;
return (temp);
}
void main()
{
auto int n;
for (n=1; n<=10; n++)
printf (“%d! = %d n”,n, factorial (n));
}
Static Variables
• Static variables are defined within individual functions and
have the same scope as automatic variables.
• Unlike automatic variables, static variables retain their
values throughout the life of the program.
• If a function is exited and re-entered at a later time, the static
variables defined within that function will retain their previous
values.
• Initial values can be included in the static variable declaration.
• Will be initialized only once.
• An example of using static variable:
• Count number of times a function is called.
Example: Static
#include <stdio.h>
void print()
{
int count = 0;
printf("Hello World!! ");
count++;
printf("is printing %d times.n",count);
}
int main()
{
int i = 0;
while(i<10)
{
print();
i++;
}
return 0;
}
Output
Hello World!! is printing 1 times.
Hello World!! is printing 1 times.
Hello World!! is printing 1 times.
Hello World!! is printing 1 times.
Hello World!! is printing 1 times.
Hello World!! is printing 1 times.
Hello World!! is printing 1 times.
Hello World!! is printing 1 times.
Hello World!! is printing 1 times.
Hello World!! is printing 1 times.
Example: Static
#include <stdio.h>
void print()
{
static int count = 0;
printf("Hello World!! ");
count++;
printf("is printing %d times.n",count);
}
int main()
{
int i=0;
while(i<10)
{
print();
i++;
}
return 0;
}
Output!
Hello World!! is printing 1 times.
Hello World!! is printing 2 times.
Hello World!! is printing 3 times.
Hello World!! is printing 4 times.
Hello World!! is printing 5 times.
Hello World!! is printing 6 times.
Hello World!! is printing 7 times.
Hello World!! is printing 8 times.
Hello World!! is printing 9 times.
Hello World!! is printing 10 times.
External Variables
• They are not confined to single functions.
• Declare them outside the function, at the
beginning.
• Their scope extends from the point of definition
through the remainder of the program.
• They may span more than one functions.
• Also called global variables.
• Alternate way of declaring global variables.
Example: Global
#include <stdio.h>
extern int count = 0;
void print()
{
printf("Hello World!! ");
count++;
}
int main()
{
int i=0;
while(i<10)
{
print();
i++;
printf("is printing %d times.n",count);
}
return 0;
}
Output!
Hello World!! is printing 1 times.
Hello World!! is printing 2 times.
Hello World!! is printing 3 times.
Hello World!! is printing 4 times.
Hello World!! is printing 5 times.
Hello World!! is printing 6 times.
Hello World!! is printing 7 times.
Hello World!! is printing 8 times.
Hello World!! is printing 9 times.
Hello World!! is printing 10 times.
Static versus Extern
#include <stdio.h>
void print()
{
static int count=0;
printf("Hello World!! ");
count++;
printf("is printing %d
times.n",count);
}
int main()
{
int i=0;
while(i<10)
{
print();
i++;
}
return 0;
}
#include <stdio.h>
extern int count=0;
void print()
{
printf("Hello World!! ");
count++;
}
int main()
{
int i=0;
while(i<10)
{
print();
i++;
printf("is printing %d
times.n",count);
}
return 0;
}
Register Variables
• These variables are stored in high-speed registers within the
CPU.
• Commonly used variables like loop variables/counters may be
declared as register variables.
• Results in increase in execution speed.
• User can suggest, but the allocation is done by the compiler.
Example Register Variables
#include<stdio.h>
int main()
{
int sum;
register int count;
for(count=0;count<20;count++)
sum=sum+count;
printf("nSum of Numbers:%d", sum);
return(0);
}

More Related Content

PPTX
Storage classes
PPTX
Storage classes in C
PPTX
STORAGE CLASS.pptx
PPTX
Storage class in c
PPTX
C language presentation
PDF
C Programming Storage classes, Recursion
DOC
Storage classess of C progamming
Storage classes
Storage classes in C
STORAGE CLASS.pptx
Storage class in c
C language presentation
C Programming Storage classes, Recursion
Storage classess of C progamming

Similar to Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD (20)

PPTX
Storage_classes_and_Scope_rules.pptx
PPT
Storage classes
DOCX
Storage class
DOC
5.program structure
PDF
Latest C Interview Questions and Answers
PPT
Storage classes
PPT
storage clas
PPTX
Storage Classes and Functions
PDF
Unit iii
DOC
What is storage class
PDF
Advanced C Programming Notes
PPT
Functions in c
PPTX
Storage classes in c language
PPTX
Storage classes in c language
PPT
S torage class in C
PPTX
Storage class
PDF
Storage classes arrays & functions in C Language
PPTX
function in c
PPT
Lecture 13 - Storage Classes
PPTX
Storage class
Storage_classes_and_Scope_rules.pptx
Storage classes
Storage class
5.program structure
Latest C Interview Questions and Answers
Storage classes
storage clas
Storage Classes and Functions
Unit iii
What is storage class
Advanced C Programming Notes
Functions in c
Storage classes in c language
Storage classes in c language
S torage class in C
Storage class
Storage classes arrays & functions in C Language
function in c
Lecture 13 - Storage Classes
Storage class
Ad

Recently uploaded (20)

PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Computer Architecture Input Output Memory.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
International_Financial_Reporting_Standa.pdf
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
My India Quiz Book_20210205121199924.pdf
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
20th Century Theater, Methods, History.pptx
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
Virtual and Augmented Reality in Current Scenario
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Complications of Minimal Access-Surgery.pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Trump Administration's workforce development strategy
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Computer Architecture Input Output Memory.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
Chinmaya Tiranga quiz Grand Finale.pdf
International_Financial_Reporting_Standa.pdf
TNA_Presentation-1-Final(SAVE)) (1).pptx
Unit 4 Computer Architecture Multicore Processor.pptx
My India Quiz Book_20210205121199924.pdf
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
20th Century Theater, Methods, History.pptx
Hazard Identification & Risk Assessment .pdf
Virtual and Augmented Reality in Current Scenario
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Complications of Minimal Access-Surgery.pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
Practical Manual AGRO-233 Principles and Practices of Natural Farming
FORM 1 BIOLOGY MIND MAPS and their schemes
Trump Administration's workforce development strategy
Ad

Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD

  • 2. Storage Class of Variables • It refers to the permanence of a variable, and its scope within a program. • Four storage class specifications in C • Automatic: auto • External: extern • Static: static • Register: register
  • 3. Automatic Variables • These are always declared within a function and are local to the function in which they are declared. • Scope is confined to that function. • This is the default storage class specification. • All variables are considered as auto unless explicitly specified otherwise. • The keyword auto is optional. • An automatic variable does not retain its value once control is transferred out of its defining function.
  • 4. Example: Auto #include <stdio.h> int factorial(int m) { auto int i; auto int temp = 1; for (i=1; i<=m; i++) temp = temp * i; return (temp); } void main() { auto int n; for (n=1; n<=10; n++) printf (“%d! = %d n”,n, factorial (n)); }
  • 5. Static Variables • Static variables are defined within individual functions and have the same scope as automatic variables. • Unlike automatic variables, static variables retain their values throughout the life of the program. • If a function is exited and re-entered at a later time, the static variables defined within that function will retain their previous values. • Initial values can be included in the static variable declaration. • Will be initialized only once. • An example of using static variable: • Count number of times a function is called.
  • 6. Example: Static #include <stdio.h> void print() { int count = 0; printf("Hello World!! "); count++; printf("is printing %d times.n",count); } int main() { int i = 0; while(i<10) { print(); i++; } return 0; } Output Hello World!! is printing 1 times. Hello World!! is printing 1 times. Hello World!! is printing 1 times. Hello World!! is printing 1 times. Hello World!! is printing 1 times. Hello World!! is printing 1 times. Hello World!! is printing 1 times. Hello World!! is printing 1 times. Hello World!! is printing 1 times. Hello World!! is printing 1 times.
  • 7. Example: Static #include <stdio.h> void print() { static int count = 0; printf("Hello World!! "); count++; printf("is printing %d times.n",count); } int main() { int i=0; while(i<10) { print(); i++; } return 0; } Output! Hello World!! is printing 1 times. Hello World!! is printing 2 times. Hello World!! is printing 3 times. Hello World!! is printing 4 times. Hello World!! is printing 5 times. Hello World!! is printing 6 times. Hello World!! is printing 7 times. Hello World!! is printing 8 times. Hello World!! is printing 9 times. Hello World!! is printing 10 times.
  • 8. External Variables • They are not confined to single functions. • Declare them outside the function, at the beginning. • Their scope extends from the point of definition through the remainder of the program. • They may span more than one functions. • Also called global variables. • Alternate way of declaring global variables.
  • 9. Example: Global #include <stdio.h> extern int count = 0; void print() { printf("Hello World!! "); count++; } int main() { int i=0; while(i<10) { print(); i++; printf("is printing %d times.n",count); } return 0; } Output! Hello World!! is printing 1 times. Hello World!! is printing 2 times. Hello World!! is printing 3 times. Hello World!! is printing 4 times. Hello World!! is printing 5 times. Hello World!! is printing 6 times. Hello World!! is printing 7 times. Hello World!! is printing 8 times. Hello World!! is printing 9 times. Hello World!! is printing 10 times.
  • 10. Static versus Extern #include <stdio.h> void print() { static int count=0; printf("Hello World!! "); count++; printf("is printing %d times.n",count); } int main() { int i=0; while(i<10) { print(); i++; } return 0; } #include <stdio.h> extern int count=0; void print() { printf("Hello World!! "); count++; } int main() { int i=0; while(i<10) { print(); i++; printf("is printing %d times.n",count); } return 0; }
  • 11. Register Variables • These variables are stored in high-speed registers within the CPU. • Commonly used variables like loop variables/counters may be declared as register variables. • Results in increase in execution speed. • User can suggest, but the allocation is done by the compiler.
  • 12. Example Register Variables #include<stdio.h> int main() { int sum; register int count; for(count=0;count<20;count++) sum=sum+count; printf("nSum of Numbers:%d", sum); return(0); }