written 7.9 years ago by | modified 2.8 years ago by |
Mumbai University> FE > Sem 2> STRUCTURED PROGRAMMING APPROACH
Marks: 4M
Year: May 2016
written 7.9 years ago by | modified 2.8 years ago by |
Mumbai University> FE > Sem 2> STRUCTURED PROGRAMMING APPROACH
Marks: 4M
Year: May 2016
written 7.9 years ago by |
The storage class of a variable is a set of properties about the variable.
The storage class of a variable determines 3 things: –
Scope: the sections of code that can use the variable. (i.e., variable visibility domain)
Linkage: how a variable can be used in a multiple source-file
program.
Storage duration: how long the variable persists in memory.
There are 4 types of storage classes:
Static Storage Class: The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.
static is initialized only once and remains into existence till the end of program.
Example:
void test();
main()
{
test();
test();
test();
}
void test()
{
static int a=0;
a=a+1;
printf(“%d\t”,a);
}
Output:
1 2 3
Extern Storage Class:
The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined. When you have multiple files and you define a global variable or function, which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.
Example:
int num=66;
void display();
main()
{
extern int num;
printf(“\nNum=%d,num”);
display();
}
void display()
{
extern int num;
printf(“\nNum=%d,num”);
}
Output:
Num=66
Num=66