SlideShare a Scribd company logo
1 of 26
Storage Classes
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
13
1
Outline
 What is Storage Classes?
 Storage Classes in C
 The auto Storage Class
 What is GlobalVariable?
 The extern Storage Class
 The static Storage Class
 The register Storage Class
What is Storage Classes?
 Storage Classes are used to describe about 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.
Storage Classes in C
 We have four different storage classes in a C program
 auto
 register
 static
 extern
Storage Classes in C (cont..)
The auto Storage Class
 This is the default storage class for all the variables
declared inside a function or a block.
 Hence, the keyword auto is rarely used while writing
programs in C language.
 Auto variables can be only accessed within the
block/function they have been declared and not
outside them (which defines their scope). Of course,
these can be accessed within nested blocks within
the parent block/function in which the auto variable
was declared.
The auto Storage Class (cont..)
 However, they can be accessed outside their scope as
well using the concept of pointers given here by
pointing to the very exact memory location where
the variables resides.
 They are assigned a garbage value by default
whenever they are declared.
The auto Storage Class (cont..)
 However, they can be accessed outside their scope as
well using the concept of pointers given here by
pointing to the very exact memory location where
the variables resides.
 They are assigned a garbage value by default
whenever they are declared.
The auto Storage Class (cont..)
{
int mount;
auto int month;
}
 The example above defines two variables with in the
same storage class. 'auto' can only be used within
functions, i.e., local variables.
The auto Storage Class (cont..)
//sum of two numbers
#include <stdio.h>
int sum(int n1, int n2){
auto int s; //declaration of auto(local) variable
s = n1+n2;
return s;
}
int main(){
int i = 2, j = 3, k;
k = sum(i, j);
printf("sum is : %dn", k);
return 0;
}
The auto Storage Class (cont..)
Output:
sum is : 5
What is Global Variable?
 A global variable is a variable which is declared
outside of all the functions. It can be accessed
throughout the program and we can change its
value anytime within any function.
Global Variable Example
#include <stdio.h>
int g;
void print(){
g = 10;
printf("g = %dn", g);
}
int main(){
g = 7;
printf("g = %dn", g);
print();
return 0;
}
Global Variable Example
Output:
g = 7
g = 10
 Here, g is the global variable defined outside of all the
functions. In the main function, its value was assigned as 7 and
in the print function as 10.
The extern Storage Class using example
We can do same example by using extern keyword
as shown below.
firstfile.c
int g = 0;
In the first program file firstfile.c, we declared a global
variable g.
The extern Storage Class (cont..)
Now, we will declare this variable 'g' as extern in a
header file firstfile.h and then include it in the second
file in which we want to use this variable.
firstfile.h
extern int g;
The extern Storage Class (cont..)
Now in the second program file secondfile.c, in order to use the
global variable 'g', we need to include the header file in it by writing
#include "firstfile.h". Here we assigned a value 4 to the variable 'g'
and thus the value of 'g' in this program becomes 4.
secondfile.c
#include "firstfile.h"
main(){
g = 4;
printf("%d", g);
}
The extern …
 extern storage class simply tells us that the variable is
defined elsewhere and not within the same block
where it is used.
 Basically, the value is assigned to it in a different block
and this can be overwritten/changed in a different block
as well.
 So an extern variable is nothing but a global variable
initialized with a legal value where it is declared in
order to be used elsewhere.
 It can be accessed within any function/block.
The extern … (cont..)
 A normal global variable can be made extern as well by
placing the ‘extern’ keyword before its
declaration/definition in any function/block.
 This basically signifies that we are not initializing a new
variable but instead we are using/accessing the global
variable only.
 The main purpose of using extern variables is that they
can be accessed between two different files which are
part of a large program.
The static Storage Class
 This storage class is used to declare static variables
which are popularly used while writing programs in C
language.
 Static variables have a property of preserving their
value even after they are out of their scope! Hence,
static variables preserve the value of their last use in
their scope.
 So we can say that they are initialized only once and
exist till the termination of the program.Thus, no new
memory is allocated because they are not re-declared.
The static Storage Class (cont..)
 Their scope is local to the function to which they were
defined.
 Global static variables can be accessed anywhere in the
program. By default, they are assigned the value 0 by
the compiler.
The static Storage Class Example
#include <stdio.h>
static int g = 5;
void fn(){
static int i = 0;
printf("g = %dt", g--);
printf("i = %dn",i++);
}
int main(){
while(g >= 2)
fn();
return 0;
}
The static Storage Class Example
Output:
g = 5 i = 0
g = 4 i = 1
g = 3 i = 2
g = 2 i = 3
 Here, g and i are the static variables in which 'g' is a global variable and
'i' is a local variable. If we had not written static before the declaration
of 'i', then everytime the function 'fn()' would have been called, 'i' would
have been declared every time with an initial value of 0 and as the
function 'fn()' would exit, it would also have got destroyed.
The register Storage Class
 This storage class declares register variables which
have the same functionality as that of the auto
variables.The only difference is that the compiler
tries to store these variables in the register of the
microprocessor if a free register is available.
 This makes the use of register variables to be much
faster than that of the variables stored in the memory
during the runtime of the program.
 If a free register is not available, these are then stored
in the memory only.
The register Storage Class (cont..)
 Usually few variables which are to be accessed very frequently
in a program are declared with the register keyword which
improves the running time of the program.
 An important and interesting point to be noted here is that
we cannot obtain the address of a register variable using
pointers.
{
register int miles;
}
Lecture 13 - Storage Classes

More Related Content

What's hot (20)

RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
C function
C functionC function
C function
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
Function in c
Function in cFunction in c
Function in c
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Pointer basics
Pointer basicsPointer basics
Pointer basics
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
C function presentation
C function presentationC function presentation
C function presentation
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
Function lecture
Function lectureFunction lecture
Function lecture
 

Similar to Lecture 13 - Storage Classes

What is storage class
What is storage classWhat is storage class
What is storage classIsha Aggarwal
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C Self employed
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and AnswersDaisyWatson5
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++Reddhi Basu
 
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
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semKavita Dagar
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variablesSaurav Kumar
 
EXTERN -- wherever u define variables, it will get access to use them
EXTERN -- wherever u define variables, it will get access to use themEXTERN -- wherever u define variables, it will get access to use them
EXTERN -- wherever u define variables, it will get access to use themAjay Chimmani
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and FunctionsJake Bond
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxCheriviralaNikhil
 

Similar to Lecture 13 - Storage Classes (20)

What is storage class
What is storage classWhat is storage class
What is storage class
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
5.program structure
5.program structure5.program structure
5.program structure
 
Storage class
Storage classStorage class
Storage class
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
Storage classes
Storage classesStorage classes
Storage classes
 
C functions list
C functions listC functions list
C functions list
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Storage classes
Storage classesStorage classes
Storage classes
 
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
 
Unit iii
Unit iiiUnit iii
Unit iii
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-sem
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 
EXTERN -- wherever u define variables, it will get access to use them
EXTERN -- wherever u define variables, it will get access to use themEXTERN -- wherever u define variables, it will get access to use them
EXTERN -- wherever u define variables, it will get access to use them
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
Storage classes
Storage classesStorage classes
Storage classes
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptx
 
SPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in CSPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in C
 

More from Md. Imran Hossain Showrov (14)

Lecture 22 - Error Handling
Lecture 22 - Error HandlingLecture 22 - Error Handling
Lecture 22 - Error Handling
 
Lecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header FileLecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header File
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions Lecture 7- Operators and Expressions
Lecture 7- Operators and Expressions
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
 
Lecture 4- Computer Software and Languages
Lecture 4- Computer Software and LanguagesLecture 4- Computer Software and Languages
Lecture 4- Computer Software and Languages
 
Lecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devicesLecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devices
 
Lecture 2 - Introductory Concepts
Lecture 2 - Introductory ConceptsLecture 2 - Introductory Concepts
Lecture 2 - Introductory Concepts
 
Lecture 1- History of C Programming
Lecture 1- History of C Programming Lecture 1- History of C Programming
Lecture 1- History of C Programming
 

Recently uploaded

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 

Recently uploaded (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

Lecture 13 - Storage Classes

  • 1. Storage Classes Md. Imran Hossain Showrov (showrovsworld@gmail.com) 13 1
  • 2. Outline  What is Storage Classes?  Storage Classes in C  The auto Storage Class  What is GlobalVariable?  The extern Storage Class  The static Storage Class  The register Storage Class
  • 3. What is Storage Classes?  Storage Classes are used to describe about 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.
  • 4. Storage Classes in C  We have four different storage classes in a C program  auto  register  static  extern
  • 5. Storage Classes in C (cont..)
  • 6. The auto Storage Class  This is the default storage class for all the variables declared inside a function or a block.  Hence, the keyword auto is rarely used while writing programs in C language.  Auto variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope). Of course, these can be accessed within nested blocks within the parent block/function in which the auto variable was declared.
  • 7. The auto Storage Class (cont..)  However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables resides.  They are assigned a garbage value by default whenever they are declared.
  • 8. The auto Storage Class (cont..)  However, they can be accessed outside their scope as well using the concept of pointers given here by pointing to the very exact memory location where the variables resides.  They are assigned a garbage value by default whenever they are declared.
  • 9. The auto Storage Class (cont..) { int mount; auto int month; }  The example above defines two variables with in the same storage class. 'auto' can only be used within functions, i.e., local variables.
  • 10. The auto Storage Class (cont..) //sum of two numbers #include <stdio.h> int sum(int n1, int n2){ auto int s; //declaration of auto(local) variable s = n1+n2; return s; } int main(){ int i = 2, j = 3, k; k = sum(i, j); printf("sum is : %dn", k); return 0; }
  • 11. The auto Storage Class (cont..) Output: sum is : 5
  • 12. What is Global Variable?  A global variable is a variable which is declared outside of all the functions. It can be accessed throughout the program and we can change its value anytime within any function.
  • 13. Global Variable Example #include <stdio.h> int g; void print(){ g = 10; printf("g = %dn", g); } int main(){ g = 7; printf("g = %dn", g); print(); return 0; }
  • 14. Global Variable Example Output: g = 7 g = 10  Here, g is the global variable defined outside of all the functions. In the main function, its value was assigned as 7 and in the print function as 10.
  • 15. The extern Storage Class using example We can do same example by using extern keyword as shown below. firstfile.c int g = 0; In the first program file firstfile.c, we declared a global variable g.
  • 16. The extern Storage Class (cont..) Now, we will declare this variable 'g' as extern in a header file firstfile.h and then include it in the second file in which we want to use this variable. firstfile.h extern int g;
  • 17. The extern Storage Class (cont..) Now in the second program file secondfile.c, in order to use the global variable 'g', we need to include the header file in it by writing #include "firstfile.h". Here we assigned a value 4 to the variable 'g' and thus the value of 'g' in this program becomes 4. secondfile.c #include "firstfile.h" main(){ g = 4; printf("%d", g); }
  • 18. The extern …  extern storage class simply tells us that the variable is defined elsewhere and not within the same block where it is used.  Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well.  So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere.  It can be accessed within any function/block.
  • 19. The extern … (cont..)  A normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block.  This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only.  The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program.
  • 20. The static Storage Class  This storage class is used to declare static variables which are popularly used while writing programs in C language.  Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope.  So we can say that they are initialized only once and exist till the termination of the program.Thus, no new memory is allocated because they are not re-declared.
  • 21. The static Storage Class (cont..)  Their scope is local to the function to which they were defined.  Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.
  • 22. The static Storage Class Example #include <stdio.h> static int g = 5; void fn(){ static int i = 0; printf("g = %dt", g--); printf("i = %dn",i++); } int main(){ while(g >= 2) fn(); return 0; }
  • 23. The static Storage Class Example Output: g = 5 i = 0 g = 4 i = 1 g = 3 i = 2 g = 2 i = 3  Here, g and i are the static variables in which 'g' is a global variable and 'i' is a local variable. If we had not written static before the declaration of 'i', then everytime the function 'fn()' would have been called, 'i' would have been declared every time with an initial value of 0 and as the function 'fn()' would exit, it would also have got destroyed.
  • 24. The register Storage Class  This storage class declares register variables which have the same functionality as that of the auto variables.The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register is available.  This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program.  If a free register is not available, these are then stored in the memory only.
  • 25. The register Storage Class (cont..)  Usually few variables which are to be accessed very frequently in a program are declared with the register keyword which improves the running time of the program.  An important and interesting point to be noted here is that we cannot obtain the address of a register variable using pointers. { register int miles; }