Notes on
Storage Classes
Instructor:
Arghodeep Paul
Firmware Engineer at BitBible Technologies Pvt. Ltd.
Content Author: Arghodeep Paul
License: OpenSource
Date: 14 July 2021
Storage Class
Storage class defines the Scope/Visibility and Lifetime of a Variable or Function.
Types
auto: The default class(int a=2 is equal as auto int a=2)
static: Instructs the compiler to keep a local variable in alive during the
runtime/lifetime of a program. By default a variable is created when control comes
into scope of a function and destroyed when going out of scope.
example
#include <stdio.h>
int main() {
void fx(){
static int n=2;
printf("Value: %dn",n);
n++;
}
fx();
fx(); // calling twice
return 0;
}
Extern: This class gives the reference of a global variable of function that defined
into another source file.
Program 1
#include <stdio.h>
extern float banglaMarks;
extern char name[30];
extern void showDetail(void);
main() {
banglaMarks=55;
showDetail();
}
Program 2
#include <stdio.h>
float banglaMarks=99.99;
char name[30]="Humayun Faridi";
void showDetail(void); // declaration
void showDetail(void){ //defination
printf("%s got %0.2f in Bangla Paper",name,banglaMarks);
}
Compile: gcc file1.c file2.c -o Main.exe
register: This will define variable that should be stored in a register instead of RAM.
You can not use & operator with this types of variables as it will not return you RAM
Address(it is stored in CPU that’s why).
Example
#include <stdio.h>
int main() {
register int a;
scanf("%d",&a);
printf("%d",a);
return 0;
}
Scope of a Function
void function(){
//This area is considered as the Scope of this function
}
C storage classes

C storage classes

  • 1.
    Notes on Storage Classes Instructor: ArghodeepPaul Firmware Engineer at BitBible Technologies Pvt. Ltd. Content Author: Arghodeep Paul License: OpenSource Date: 14 July 2021
  • 2.
    Storage Class Storage classdefines the Scope/Visibility and Lifetime of a Variable or Function. Types auto: The default class(int a=2 is equal as auto int a=2) static: Instructs the compiler to keep a local variable in alive during the runtime/lifetime of a program. By default a variable is created when control comes into scope of a function and destroyed when going out of scope. example #include <stdio.h> int main() { void fx(){ static int n=2; printf("Value: %dn",n); n++; } fx(); fx(); // calling twice return 0; } Extern: This class gives the reference of a global variable of function that defined into another source file. Program 1 #include <stdio.h> extern float banglaMarks; extern char name[30]; extern void showDetail(void); main() { banglaMarks=55; showDetail(); }
  • 3.
    Program 2 #include <stdio.h> floatbanglaMarks=99.99; char name[30]="Humayun Faridi"; void showDetail(void); // declaration void showDetail(void){ //defination printf("%s got %0.2f in Bangla Paper",name,banglaMarks); } Compile: gcc file1.c file2.c -o Main.exe register: This will define variable that should be stored in a register instead of RAM. You can not use & operator with this types of variables as it will not return you RAM Address(it is stored in CPU that’s why). Example #include <stdio.h> int main() { register int a; scanf("%d",&a); printf("%d",a); return 0; } Scope of a Function void function(){ //This area is considered as the Scope of this function }