The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
A storage class defines the scope, visibility and life-time of variables.
There are four types of storage classes in c programming. They are
Keyword | Storage Classes |
---|---|
auto | automatic storage class |
static | static storage class |
register | register storage class |
extern | external storage class |
We already defined that a scope of a variable is defined by its storage classes.
A scope is defined as the area in which the declared variable is available.In c programing we can encounter five different types of scopes. They are
Let's workout a program to explain all 5 different scope in storage class.
#include <stdio.h> void display(int num); // here 'num' has prototype scope static void fun(); int main() { int num=2; void display(int d){ // here 'display' has program scope printf("\n%d", d); } display(1); fun(); bark:{ //here 'bark' has function scope int i = 0; printf("\nBow Bow"); if(i = 1){ return 0; } i++; } goto bark; return 0; } static void fun(){ //here 'fun' has file scope int i = 0; //here 'i' has block scope printf("\n%d", i); i++; }
display has a program scope because all non-static function have program scope.
fun has a file scope because all static function can only accessed within a file.
bark is a label which has a function scope because there can be only one bark label within a function hence it has function scope.
i has a block scope because every function can have a variable with name i
num has a prototype scope because one cannot declare a variable j within the same function.
We already defined that visibility of a variable is defined by its storage class.
Visibility is the way of hidding or showing a variable inside or outside a scope. Visibility is otherwise said to be accessibility.
Let's workout a simple program to explain visibility of a variable.
#include <stdio.h> int main() { int i = 9; //visible inside all function. void display(){ //visible inside this function only. int j = 10; printf("%d",i); } display(); printf("%d",j); return 0; }
We already defined that life-time of a variable is defined by its storage class.
Life-time is defined as a period of time in which the variable lives. There are three types of life-times in storage classes. They are
Let's workout a simple program to explain all 3 life-time in storage class.
#include <stdio.h> int main() { int *numbers = (int*)malloc(4* sizeof(int)); // *number act as a automatic and dynamic as well static int i; // i act as a static variable numbers[0] = 0; numbers[1] = 1; numbers[2] = 2; numbers[3] = 3; free(numbers); printf("\nStored integers are "); for(i = 0; i < 4; i++) printf("\numbers[%d] = %d ", i, numbers[i]); return 0; }
Here i has static life-time because its life-time is within a program.
*numbers has automatic life-time because its life-time starts when *number variable is allocated by malloc() inbuilt memory function.
*numbers has dynamic life-time too because free() memory function can be used any time to clear data in allocated memory.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019