
Jasmine Grover Content Strategy Manager
Content Strategy Manager
Storage Classes in C play a pivotal role in determining the characteristics of variables and functions, encompassing essential aspects like visibility, scope, and lifetime. These classes serve as a roadmap to understanding the presence and behavior of specific variables throughout the runtime of a program.
- By categorizing variables and functions into different storage classes, C provides a flexible framework for managing memory and optimizing resource allocation.
- The features of a variable/function which includes visibility, scope, and lifetime are described by storage class C.
- It aids in tracking the presence of a specific variable throughout the program's execution.
Table of Content |
Key Terms: Storage Class, Automatic Storage Class, External Storage Class, Static Storage Class, Register Storage Class.
Types of Storage Classes in C
[Click Here for Sample Questions]
There are 4 types of storage classes in C such as:
- Automatic: Automatic is the default storage class where all the variables are declared inside a function or a block. These auto variables can be accessed within the block/function, not outside the block.
- External: These are the variables that are defined elsewhere and not within the same block.
- Static: These are the storage class that is used to declare static variables while writing programs in C language.
- Register: These storage classes declare register variables with similar functionality to auto variables.
Syntax
[Click Here for Sample Questions]
To specify a variable for a storage class, the following is the syntax:
storage_class var_data_type var_name;
Read More: Tree Topology
Uses of storage class in C
[Click Here for Sample Questions]
The storage classes in a C program have two properties such as storage class and type.
- Type: It refers to any given variable’s data type.
- Storage: It determines the variable’s lifetime, visibility, and also its scope.
Summary of storage classes in C
[Click Here for Sample Questions]
The table below shows the summary of storage classes in C:
Class | Name of Class | Place of Storage | Scope | Default Value | Lifetime |
---|---|---|---|---|---|
auto | Automatic | RAM | Local | Garbage Value | Within a function |
extern | External | RAM | Global | Zero | Till the main program ends. It can be declared at any point within a program. |
static | Static | RAM | Local | Zero | Till the main program ends. It preserves the value between different function calls. |
register | Register | Register | Local | Garbage Value | Within the function |
Automatic Storage Class
[Click Here for Sample Questions]
These are also known as the auto storage class, serving as the default storage class for all locally scoped variables.
For example,
{
int mount;
auto int month;
}
The features of the Automatic Storage class are mentioned below:
- During the runtime, the allocation of memory for these variables occurs automatically.
- The scope for automatic variables is limited to a block where they are being defined.
- The visibility of these variables is limited to the block in which they are being defined.
- By default, the initialization of these variables is a garbage value.
- The memory which is assigned to an automatic variable is free when it exits from a block.
Read More: Introduction To Array
External Storage Class
[Click Here for Sample Questions]
These are also known as extern storage classes, where reference is given to any global variable. While using the external class the variables cannot be initialized. An extern in any file would be used for providing the reference of the defined function or variable.
The most common use of the extern modifier is having two or more files that are shared over a similar global variable or function.
The features of the External Storage class are mentioned below:
- One cannot perform the initialization of an external variable with any given method or block but can perform the initialization of an external variable globally.
- The role of the external storage class is to indicate to the compiler that a variable described as "extern" has been declared.
- If a variable is declared as external to the compiler it will start searching for that variable.
Static Storage Class
[Click Here for Sample Questions]
This type of class gives instruction to a compiler to give local variables around during the program’s lifetime- instead of creating it.
The features of the Static Storage class are mentioned below:
- Static variables are defined as specifiers capable of retaining their assigned values between different function calls.
- These are visible to block or the function which has been defined.
- By default, the initial value of a static integral is 0 else it is null.
- In the case of static variables, static keywords are used.
Also Read:
Register Storage Class
[Click Here for Sample Questions]
The register storage class is defined for the local variable that is stored in the register which means that the maximum size of this variable is equal to that of the register size.
Things to Remember
- Storage classes in C determine key attributes of variables, such as their visibility, scope, and lifetime, crucial for efficient memory management.
- The scope of a variable dictates where it can be accessed, while visibility refers to its accessibility within different parts of a program.
- By classifying variables into different storage classes—automatic, external, static, and register—C enables flexible memory allocation and resource optimization.
- The 'auto' storage class is the default, local to a function or block, with memory allocated and deallocated automatically upon entry and exit.
- The 'extern' class references global variables defined elsewhere, aiding code sharing across files, though initialization is limited.
- The 'Static' class maintains variable values between function calls, limiting visibility to the block or function in which they're defined.
- The 'Register' class denotes local variables stored in registers, potentially optimizing access times, albeit with limited availability due to register constraints.
- The default initial value for external integral value is going to be 0, else null.
- To declare a variable's storage class, use the syntax: storage_class data_type variable_name;
Sample Questions
Ques. Difference between a type and a storage class? (1 Mark)
Ans. The storage class will determine a variable’s lifetime, visibility, and also its scope whereas a type would refer to a variable’s data type.
Ques. What is the syntax for extern in C and give an example? (2 Marks)
Ans. Below is the provided syntax to define an external variable in C:
extern data_type variable_name;
Ex:
#include <stdio.h>
extern int a; // int var; -> declaration and definition
// extern int var; -> declaration
int main()
{
printf ("%d", a);
return 0;
}
Ques. What is an auto storage class, give an example. (1 Mark)
Ans. The default storage class for all local variables is known as the auto storage class.
{
int mount;
auto int month;
}
Ques. What is a register storage class, give an example. (1 Mark)
Ans. It is used to define the local variables that should be stored in the register.
{
register int miles;
}
Ques. What is a static Storage Class, give an example. (3 Marks)
Ans. The static storage class directs the compiler to retain local variables throughout the program's lifespan, rather than creating them anew.
#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
main() {
while(count--) {
func();
}
return 0;
}
/* function definition */
void func( void ) {
static int i = 5; /* local static variable */
i++;
printf("i is %d and count is %d\n", i, count);
}
Ques. What are the types of storage classes in C? (2 Marks)
Ans. The different types of storage classes in C are:
- auto
- register
- static
- extern
Ques. What are the different types of built-in operators? (2 Marks)
Ans. There are the following types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Misc Operators
Ques. Does a register variable outperform a local variable in terms of speed? (1 Mark)
Ans. The register helps the user to declare the register variables, which are actually supposed to be faster compared to local variables. These days compiler is very commendable at the optimization of codes.
Ques. What type of variables are stored in the storage classes? (2 Marks)
Ans. The local variables are the variables that are declared inside any given block which are known as automatic variables whereas the storage classes store the local variable but can give reference to a global variable. The global variable is declared outside any function that is accessible to all the functions present in a program.
Ques. What would be the output of the program? (2 Marks)
#include<stdio.h>
int func(){
static int a=0;
int b=0;
a++;
b++;
printf(“a= %d and b= %d\n”, a, b);
}
int main() {
func();
func();
func();
return 0;
}
i). a= 2 and b= 1
a= 2 and b= 1
ii). a= 1 and b= 2
a= 2 and b= 1
a= 3 and b= 2
iii) a= 1 and b= 2
a= 2 and b= 2
a= 3 and b= 1
iv) a= 1 and b= 1
a= 2 and b= 1
a= 3 and b= 1
Ans. The output of the program is mentioned below:
a= 1 and b= 1
a= 2 and b= 1
a= 3 and b= 1
For Latest Updates on Upcoming Board Exams, Click Here: https://t.me/class_10_12_board_updates
Check-Out:
Comments