C Programming
Storage Classes
"Defined path for a Stable career in Core Technologies“
hello@etechprowess.com
INDEX
❑ Storage Classes ❑ Storage Class : static
❑ Types of Storage Classes ❑ Storage Class : register
❑ Declaration using Storage Classes ❑ Use of Storage Class
❑ Storage Class : auto
❑ Storage Class : extern
❑ Properties
❑ Global variables
❑ Local Variables
❑Difference between Global & Local Variables
🌐 etechprowess.com
Storage Classes
• Any programming language mainly involves variables to deal with data.
• Every variable in C has two properties; type and storage classes.
• Among them, the type refers to the data type of the variable, and storage
classes in C determine the scope, lifetime, and visibility of the variable.
• Storage classes in C are used to find the lifetime, visibility, memory
location, and initial value of a variable.
• In simple words, a storage class in C is used to represent the information
of a variable.
What Are Storage Classes
• Storage classes in C allocate the storage area of a variable that will be
retained in the memory.
• They are stored in the RAM of a system.
• Apart from the storage space, they determine the scope of a variable.
• Variables in the C program are stored in a physical location in the random
memory, which is mainly the device’s memory and CPU registers.
• Storage classes in C also define the lifetime of the variable and term it as
'local' or 'global'.
• Storage classes are also useful to define the initial value of the variable.
•.
Types of Storage Classes
• There are primarily four storage classes in C:
• auto
Storage class Purpose
• extern
• register auto It is a default storage class.
• static extern It is a global variable.
• Even 'typedef' is one more It is a local variable which is capable of
storage class specifier used static returning a value even when control is
in the syntactic form, but transferred to the function call.
does not reserve storage. It is a variable which is stored inside a
register
Register.
Declaration using Storage Classes
• The four storage classes in C are declared in a block or program with the
storage class specifiers, auto, register, extern, static.
• The specifiers instruct the compiler on storing the variables.
• There is a key difference between defining and declaring a variable.
• Defining a variable is about allocating the memory for the variable and
declaring it means initializing it with a value.
• Syntax:
• storage_class_specifier data_type variable_name;
Declaration using Storage Classes
• Special Case: When no storage class specifier is declared or defined in a program
• There is at least one storage class specifier is given in the variable
declaration.
• But, in case, there is no storage class specifier specified, the following
rules are followed:
• Variables declared within a function are considered as auto.
• Functions declared within a function are considered as an extern.
• Variables and functions declared outside a function are considered
static, with external linkage.
Storage Class : auto
• A variable declared inside a function without any storage class
specification, is by default an automatic variable.
• They are created when a function is called and are destroyed
automatically when the function's execution is completed.
• Automatic variables can also be called local variables.
• Scope: Variable defined with auto storage class are local to the function
block inside which they are defined.
• Default Initial Value: Any random value i.e garbage value.
• Lifetime: Till the end of the function/method block where the variable is
defined.
Storage Class : auto
• #include<stdio.h>
void main()
{
int detail;
// or
auto int details; //Both are same
}
Storage Class : extern
• A variable that is declared outside any function is a Global Variable.
• Global variables remain available throughout the program execution.
• One important thing to remember about global variable is that their
values can be changed by any function in the program.
• Scope: Global i.e. everywhere in the program. These variables are not
bound by any function, they are available everywhere.
• Default initial value: 0(zero).
• Lifetime: Till the program doesn't finish its execution, you can access
global variables.
Storage Class : extern
• extern keyword
• The extern keyword is used with a variable to inform the compiler that
this variable is declared somewhere else.
• The extern declaration does not allocate storage for variables.
• Note: Declaring the storage class as global or external for all the variables
in a program can waste a lot of memory space because these variables
have a lifetime till the end of the program. Thus, variables, which are not
needed till the end of the program, will still occupy the memory and thus,
memory will be wasted.
Example – Global Variable
Program to use global variables
• #include<stdio.h> • void simple_int(int amount, int period)
#include<math.h> {
void simple_int(int amount, int period); rate = 20;
void compound_int(int amount, int period); float interest = (amount * period *rate)
int rate; // global variable /100.0;
printf("\n Simple interest is %.2f",
void main() interest);
{ }
rate = 10; void compound_int(int amount, int period)
int amount = 700, period = 2; {
printf("\n Rate of interest is %d\n", rate);
simple_int(amount, period); float interest = amount * pow((1 +
compound_int(amount, period); (rate/100.0)), period) - amount;
} printf("\n Compound interest is %.2f",
interest);
}
Properties of Local & Global Variables
• To implement a global variable following properties of global variables are
to be considered:
• The scope of the global variable also starts from the point where we declare it.
• Re-definition of Global variables are not allowed.
• We cannot directly assign one "Global variable" to another "Global variable"
outside the function.
• To implement a local variable below properties of local variables is to be
considered:
• Multiple declarations of local variables with the same name and in the same scope
are not allowed.
• Scope of the local variable starts from the point where we declared.
Global Variable
• The scope of the global variable also starts from the point where we
declare it.
• Example:
• #include <stdio.h>
int main()
{
printf("%d", age);
return 0;
}
int age = 10; // global variable declaration
void display(){
printf("Value of global variable is %d", age);
Global Variable
• Re-definition of Global variables are not allowed.
• Example:
• #include <stdio.h>
int value = 10;
value = 20; // not valid
int main()
{
printf("%d", value);
return 0;
}
Global Variable
• We cannot directly assign one "Global variable" to another "Global
variable" outside the function.
• Example:
• #include <stdio.h>
int value = 10;
int value2 = value; // invalid initialization
int main()
{
printf("%d", value);
return 0;
}
Local Variables
• Multiple declarations of local variables with the same name and in the
same scope are not allowed.
• In the below example, we are trying to declare a Local variable with the
same name more than once, so we will get compilation errors.
• Example:
• #include <stdio.h>
int main()
{
int value = 10;
int value = 20;
printf("%d", value);
return 0;
}
Local Variables
• The below program will give a compilation error because the scope of the
local variable starts from the point where we declared.
• In the below example, we are trying to access a global variable before we
are declaring it, so we get the compilation error.
• Example:
• #include <stdio.h>
int main()
{
printf("%d", value);
int value; // Scope is from this point to end of the block
return 0;
}
Storage Class : extern
• Problem when extern is not used
• int main()
{
a = 10; //Error: cannot find definition of variable 'a’
printf("%d", a);
}
• Example using extern in same file
• int main()
{
extern int x; //informs the compiler that it is defined somewhere else
x = 10;
printf("%d", x);
}
int x; //Global variable x
Local v/s Global Variable
Local variable Global variable
• Variables declared inside the • Variables declared outside the
function function
• Scope is throughout the block • Scope is throughout the program
• Lifetime is throughout the function • Lifetime is throughout the program
• Memory allocated in the stack • Memory allocated in the .data
frame of the function in which it is section of a program and it is valid
declared, and it is valid till the till the program finishes execution
function finishes execution
• Can only be accessed from inside • Can be accessed by all the
the block in which we declared it functions in the program
Local v/s Global Variable
Local variable Global variable
• Scope starts from the point where • Same as local variable
it is declared. If we try to access it
before declaring, then compilation
error will be thrown.
• #include <stdio.h> • #include <stdio.h>
void main() // Global variable
{ int a = 10;
// Local variable void main()
int a = 10; {
printf("Printing in main %d", a); printf("Printing in main %d", a);
} }
Storage Class : static
• A static variable tells the compiler to persist/save the variable until the
end of program.
• Instead of creating and destroying a variable every time when it comes
into and goes out of scope, static variable is initialized only once and
remains into existence till the end of the program.
• A static variable can either be internal or external depending upon the
place of declaration.
• Scope of internal static variable remains inside the function in which it is
defined.
• External static variables remain restricted to scope of file in which they are
declared.
Storage Class : static
• Scope: Local to the block in which the variable is defined
• Default initial value: 0(Zero).
• Lifetime: Till the whole program doesn't finish its execution.
Storage Class : static
Program to use static variables
• #include<stdio.h> • void book_seats(int nos)
void book_seats(int); {
int main() static int seats = 10; //a static
{ variable
int num_of_seats; seats = seats - nos;
char choice ; printf("Available seats:%d\n\n",seats);
while(choice != 'n’){ //add code to stop booking tickets
printf("Enter how many seats to be when all are sold out
booked : "); }
scanf("%d",&num_of_seats);
book_seats(num_of_seats);
printf("Do you want to continue?: ");
scanf(" %c", &choice); //space before %c
}
}
Storage Class : register
• Register variables inform the compiler to store the variable in CPU register
instead of memory.
• Register variables have faster accessibility than a normal variable.
• Generally, the frequently used variables are kept in registers.
• Although only a few variables can be placed inside registers.
• One application of register storage class can be in using loops, where the
variable gets used a number of times in the program, in a very short span
of time.
• NOTE: We can never get the address of such variables.
Storage Class : register
• Scope: Local to the function in which it is declared.
• Default initial value: Any random value i.e garbage value
• Lifetime: Till the end of function/method block, in which the variable is
defined.
• Note:
• Even though if the storage class of a variable is declared as register, it cannot be surely said that the value of
the variable would be stored in a register.
• Also, CPU registers are limited & are meant to do a lot of important work.
• Thus, sometimes they may not be free.
• In such scenario, the variable works as if its storage class is auto.
Use of Storage Class
• To improve the speed of execution of the program and to carefully use the
memory space occupied by the variables, following points should be kept
in mind while using storage classes:
• static storage class should be used only when the value of the variable is required
to remain same every time, it is called using different function calls.
• register storage class should be used only for those variables that are used in a
program very often. CPU registers are limited and thus should be used carefully.
• external or global storage class should be used only for those variables that are
being used by almost all the functions in the program.
• If a variable does not have the purpose of any of the above mentioned storage
classes, then the automatic storage class should be used.
THANK YOU
eTECH Prowess
Phone:+91 7676651416, 080 4890 919
Email:hello@etechprowess.com
http://etechprowess.com