C Tutorial
Storage class determins the variables storage location, scope and lifetime.
Storage locations could be memory or CPU registers.
Variable Scope is the visibility level of the variable like local or global etc..
lifetime of variable means duration between creation and destruction of variable.
auto
register
extern
static
<storage-class> <data-type> <variable-name>;
Automatic storage class takes default storage class are local to function or block.
default storage class is the automatic storage class for the variables defined inside function or block.
auto storage class variables scope is local to function or block and destroyed when exit of the function or block.
#include<stdio.h> void main() { auto int num = 10; { auto int num = 15; { printf("\nnum: %d", num); } printf("\nnum: %d", num); } printf("\nnum:%d",num); }
Output:
$ cc storage_class.c $ ./a.out num: 15 num: 15 num:10
#include<stdio.h> void main() { auto int num = 10; { auto int num = 15; { int num = 25; printf("\nnum: %d", num); } printf("\nnum: %d", num); } printf("\nnum:%d",num); }
Output:
$ cc storage_class.c $ ./a.out num: 25 num: 15 num:10
C register storage class is similer like auto storage class but difference is value of the variable will be stored in CPU registers.
register storage class is used for the variables which are accessed frequently in the c program which avoids swapping time to relocate variable value from memory to CPU registers.
Compilation error will be raised if we use pointer with register storage class
#include<stdio.h> void main() { register int num = 10; { register int num = 15; { int num = 25; printf("\nnum: %d", num); } printf("\nnum: %d", num); } printf("\nnum:%d",num); }
Output:
$ cc storage_class.c $ ./a.out num: 25 num: 15 num:10
Stotic variables can be local to function, block or file and not like global variables visible to outside function or file.
C compiler creates permanent storage for static storage class variables remains visible local to the function or block or file.
#include<stdio.h> static int num = 5; void main() { int i=0; for(i=0;i<3;i++) { static int num = 15; { printf("\nblock- num: %d", num); num ++; } } printf("\nglobal- num: %d", num); num++; printf("\nglobal- num: %d", num); }
Output:
$ cc storage_class.c $ ./a.out block- num: 15 block- num: 16 block- num: 17 global- num: 5 global- num: 6
external storage class is used to link the variables which are declared else where in the program.
It Provides external linkage to the variables.
Variable initialization is not allowed with extern storage specifier.
When we use extern specifier, no storage is allocated and also assumed that variable is already declared else where in the program
#include<stdio.h> extern int num; void main() { int i = 10; { printf("\nnum:%d", num); } printf("\nnum:%d",num); } int num =25;
Output:
$ cc storage_class.c $ ./a.out num: 25 num: 25
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page