Storage Classes in C
Chapter 4: Programming Basics
Unit 1
Chapter 1: Computer Basic*
*CS101 Computer Basic is a small separate course prerequisite for KCS101/KCS201 Programming for Problem Solving – Using C
Chapter 2: Terminology
Chapter 3: Idea of Algorithm
Chapter 4: Programming Basics
- Introduction to C Language
- Basic Structure of C Program
- Writing and executing the first C program
- Errors in C Programming
- Object Code and Executable Code
- Components of C Language
- Standard Input-Output in C Programming
- Fundamental Data Types
- Variables and Memory Locations
- Storage Classes
Unit 2
Chapter 5: Arithmetic Expressions and Precedence
Chapter 6: Conditional Branching
Unit 3
Chapter 7: Iteration and Loops
Chapter 8: Functions
Unit 4
Chapter 9: Arrays, Structure, Union, and Enumeration
Chapter 10: Basic Algorithms and their Complexity Coexistence
Unit 5
Chapter 11: Pointers
Chapter 12: File Handling
Chapter 13: Macros & Command-Line Arguments
Appendix
KCS151/KCS251 PPS Lab Exercise
PPS Previous Year Question Papers
Storage Class
- Automatic Storage Class
- Register Storage Class
- Static Storage Class
- External Storage Class
Before discussing Storage Classes in deep, it is better to dig into the environment in which they are used.
Scope
Block (Local) Scope
File (Global) Scope
A large program can have multiple source files. The file scope covers the entire source file with the files included in it. An object with file scope can be accessed within the file in which it is declared from its declaration point to the end of the file. Objects with file scope are sometimes referred to as global objects.
Extent
Automatic Extent
Static Extent
Dynamic Extent
Dynamic memory allocation is used to create a single large block of memory at run time. It can be achieved by using C library functions such as malloc, calloc, realloc, etc. Memory created by these functions has a dynamic extent. The objects can acquire such types of memory and free it at run time as per the requirement.
Linkage
Internal Linkage
External Linkage
An object with external linkage declared in one file can also be accessed from other linked files of the program as well as within the file it is declared.
Memory Location
At the time of program execution, memory is assigned to these objects. They can be stored in the program stack, data segment, heap or CPU registers. The storage class defines the memory location to store these objects.
Default Value
The storage class decides the initial value of the objects. However, an object can be initialized with a value at the time of its declaration but if no value is assigned to the object, it stored its initial value decided by the storage class. It may be zero or a garbage value.
Automatic Storage Class
/*
File: Auto.c
Author: Aditya Saini
Date: July 24, 2019
Description: Program to demonstrate the use of Automatic Storage Class.
*/
#include
int main (void)
{
int i;
for ( i = 1; i < = 4; i++ )
{
int x = 10;
printf ( "Value of x in iteration %d is : %d\n", i, x );
x = x + 10;
}
return 0;
};
Try it yourself
Register Storage Class
/*
File: Register.c
Author: Aditya Saini
Date: July 24, 2019
Description: Program to demonstrate the use of Automatic Storage Class.
*/
#include
int main (void)
{
register int i;
for ( i = 1; i <= 4; i++ )
{
int x = 10;
printf ( "Value of x in iteration %d is : %d\n", i, x );
x = x + 10;
}
return 0;
};
Try it yourself
Static Storage Class
Example Static Storage Class with Block Scope:
/*
File: b_static.c
Author: Aditya Saini
Date: July 24, 2019
Description: Program to demonstrate the use of Static Storage Class with Block Scope.
*/
#include
int main (void)
{
int i;
for ( i = 1; i <= 4; i++ )
{
static int x = 10;
printf ( "Value of x in iteration %d is : %d\n", i, x );
x = x + 10;
}
return 0;
};
Try it yourself
Example Static Storage Class with File Scope:
/*
File: f_static.c
Author: Aditya Saini
Date: July 24, 2019
Description: Program to demonstrate the use of Static Storage Class with File Scope.
*/
#include
void fun (void);
static int x = 7;
int main (void)
{
printf ( "Value of x in main is : %d\n", x );
fun ( );
printf ( "Value of x in main is : %d\n", x );
return 0;
};
void fun (void)
{
x++;
printf ( "Value of x in fun is : %d\n", x );
return;
};
Try it yourself
External Storage Class
It is common, on large projects, to decompose the project into many source files. These files are compiled separately and linked together to form the complete software as a single unit.
Objects of extern storage class defined in one source file can also be accessed from all the source files in which they are declared. These objects are generally declared at the global level with the keyword extern. These objects have file scope with static extent but the linkage is external. The memory location of these objects is the data segment and the default value of these objects is zero if not initialized.
This design leads to a confusion that which file defines the object and which are simply declaring the variable? Actually, it depends on whether the object is initialized or not.
- If the object is not initialized, then the first declaration seen by the linkage editor or linker is considered as a definition and all others as a declaration to reference it.
- If the object is initialized somewhere, then it is considered to be as defining declaration.
/*
File: f_extern.c
Author: Aditya Saini
Date: July 24, 2019
Description: Program to demonstrate the use of Extern Storage Class.
*/
#include
extern int extern_var;
int main (void)
{
printf ("External Variable: %d", extern_var);
return 0;
};
/*
File: s_extern.c
Author: Aditya Saini
Date: July 24, 2019
Description: Program to demonstrate the use of Extern Storage Class.
*/
#include
extern_var = 10;
Note: In order to compile and run the above code, you need a local IDE.
Summary
Storage Class | Keyword | Scope | Extent | Linkage | Memory Location | Lifetime | Default Value |
---|---|---|---|---|---|---|---|
Automatic | auto or none | Block | Automatic | Internal | Program Stack | Till end of Block | Garbage |
Register | register | Block | Automatic | Internal | Preferably CPU Register or Program Stack | Till end of Block | Garbage |
Static | static | Block or File | Static | Internal | Data Segment | Till end of the Program | Zero |
Extern | extern or none | File | Static | External | Data Segment | Till end of the Program | Zero |