Presiding Officer Training module 2024 lok sabha elections
STORAGE CLASS.pptx
1. • A storage class defines the scope , visibility and
life time of variables and/or functions within a C
Program.
• There are following storage classes which can be
used in a C Program
1. Automatic variables
2. Register variables
3. Static variables
4. Extern variables
2. • Life Time – Life time of any variable is the time for which
the particular variable outlives in memory during running
of the program.
• Scope –A variable may be in the memory but may not be
accessible though. So, the area of our program where we
can actually access our entity (variable in this case) is the
scope of that variable.
• . The visibility of a variable determines how much of the
rest of the program can access that variable. You can
arrange that a variable is visible only within one part of one
function, or in one function, or in one source file, or
anywhere in the program. (
•
3. Automatic variable - Storage Class
• It is declared inside function where it is used
• They are created when function is called and destroyed
automatically when the function is exited
• They are local to function and called as private variables
• It is also called as local or internal variables
• Main()
{
int number;
}
• auto is the default storage class for all local variables no
need to use the keyword auto
4. Illustration how automatic variables
work
Void function1(void)
Void function2(void)
Main()
{
int m = 1000;
function2();
printf( “%dn”, m);
}
Void function1(void)
{
int m = 10;
printf(“%dn”, m);
}
Void function2(void)
{
int m= 100;
function1();
printf(“%d n”,m);
}
Output
10
100
1000
5. External variable
• These variables are both alive and active throughout the entire program
• It is also called global variables
• It can be access by any function in the program
• It is declared outside the function
• Extern is the keyword used for this type of class
• When you have multiple files and you define a global variable or function,
which will also be used in other files, then extern will be used in another
file to provide the reference of defined variable or function. Just for
understanding, extern is used to declare a global variable or function in
another file.
• The extern modifier is most commonly used when there are two or more
files sharing the same global variables or functions as explained below.
•
6. #include <stdio.h>
int count ;
extern void write_extern();
main()
{ count = 5;
write_extern();
}
Main.c
#include <stdio.h>
extern int count;
void write_extern(void)
{ printf("count is %dn", count);
}
Suppot.c
Compile
Gcc main.c support.c
Output is 5
7. Static - Storage Class
• It persists at the function until the end of the program
• The keyword static is used for declaration
– Static int x;
– Static float y;
• Static may be either internal type or external type
• Internal means it is declared inside the function
• The scope is us up to the end of the function
• It is similar to auto variable
• It is used to retain the values between function calls
8. Program to illustrate the properties
of a static variables
Void stat(void)
Main()
{
int il
for (i=1;i<=3;i++)
stat();
}
Void stat(void)
{static int x=0;
X=x+1;
Printf(“x=%dn”,x);
Output
X=1
X=2
X=3
9. Register - Storage Class
• Register is used to define local variables that should be stored in a register
instead of RAM.
• This means that the variable has a maximum size equal to the register size
(usually one word) and cant have the unary '&' operator applied to it (as it
does not have a memory location).
{
register int Miles;
}
• Register should only be used for variables that require quick access - such
as counters.
• It should also be noted that defining 'register' goes not mean that the
variable will be stored in a register.
• It means that it MIGHT be stored in a register - depending on hardware
and implementation restrictions.
10. Type casting
• C allows programmers to perform typecasting by placing the type name in parentheses and
placing this in front of the value.
•
• For instance
•
• main()
{
float a;
a = (float)5 / 3;
}
•
• gives result as 1.666666 . This is because the integer 5 is converted to floating point value
before division and the operation between float and integer results in float.
•
• From the above it is clear that the usage of typecasting is to make a variable of one type, act
like another type for one single operation.
• So by using this ability of typecasting it is possible for create ASCII characters by typecasting
integer to its character equivalent.
11. Type definition
• This feature allows user to define new data
type that are equivalent to existing data types
• Once the new data type has been established
, the new variable, array and structure can be
declared in terms of the new data type
• The format is
– Typedef type new-type
– Eg: typedef int age
– age is also a datatype which hold int value
12. • Age male , female;
• male is a variable name with the data type of
age which equivalent to the int data type
• This type def is used with structure also