storage classes of c will provides following information to the compiler i,e
This storage class variables will created automatically and destroyed automatically.
Type | Scope | Life | DEFAULT | Value |
auto | body | body | Garbage | value |
static | function | program | 0 | |
Extern | program | program | 0 | |
All functions register body body garbage value. In ‘c’ programming language , there are 4 types of scopes are available. ie
int a; short int a; signed short int a; auto signed short int a;
Storage sign size Type variable.
It is a special kind of variables which stores in CPU registers.
void main() { register int a=10; ++a; printf(“\na=%d”,a); printf(“\n Enter a value:”); scanf(“%d”,&a); ++a; printf(“\na=%d”,a); } // input value is 20 o/p : Error must take address of a memory location
{ Auto int a=5; ++a; Printf(“\n a=%d”,a); } Void main() { abc() abc(); abc(); } o/p : a=6 a=6; a=8;
{ auto int a=5; ++a; printf(“\n a=%d”,a); { int b=10; ++a; ++b; printf(“\n a=%d b=%d”,a,b); } ++a; printf(“\n a=%d”,a); } o/p: a=6; a=7; a=8;b=11
{ int a=10; --a; printf(“\n a=%d”,a); auto int a=5; int b=15; ++a; --b; printf(“\n a=%d b=%d”, a,b); } --a; ++b; printf(“\n a=%d b=%d”,a,b); } o/p: Error undefined symbol ‘b’ When we are creating a variable directly in subbody, then scope is restricted within the sub body only. That’s why, we can’t access the variable in main body.
{ int a=5; static int s=5; ++a; ++s; printf(“\n a=%d s=%d”,a,s); }
{ abc(); abc(); abc(); o/p: a=6 s=6 a=6 s=7 a=6 s=8 When we are working with the static variable, it will be created only once, when we are calling f’n for the first time and remaining all calls, same values are used By default value of the static variable is 0 if we are not initialize the data, if it is auto variable by default value is garbage.
{ static int s=10; --s; printf(“-n statics : %d”,s); { static int s=2-; --s; printf(“\n static 2: %d”,s); } }
{ abc(); abc(); abc(); output : static1=9 static 2=9 static 1:8 static 2:18 static 1 : 7 static 2 :17
{ static int s=10; ++s; printf(“\n s=%d”,s); } void main() { abc(); abc(); printf(“\n s=%d”,s); } o/p : Error undefined symbol ‘s’
int g; // global variable void abc() { ++g; } void xyz() { ++g; void main() { ++g; abc(); xyz(); prointf(“g=%d”,g); } o/p : g=3 Void abc() { ++g; } int g=5; void main() { ++g; abc(); printf(“g=%d”,g); } o/p : Error undefined symbol ‘g’
void abc() { ++g; // declaration } void main() { ++g; abc(); printf(“g=%d”,g); } o/p : g=7 The basic difference b/w declaration of a global variable and definition is :
void abc() { ++g; } void xyz(); ++g; } void main() { ++g; abc(); xyz(); printf(“g=%d”,g); } o/p : Error undefined symbol ‘g’
void main() { // extern int g; // local declaration ++g; } void xyz() { //extern int g; // local declaration ++g; } void main() { // extern int g; // local declaration ++g; abc(); xyz(); printf(“g=%d”,g) ; } int g; o.p : g=3 void main() { ++g; } void main() { ++g; abc(); printf(“g=%d”,g); } o/p : Error undefined symbol = g(Liniking error)
Extern int g=5; void xyz(); { --g; } void main() { --g; xyz(); printf(“g%d”,g); } int g o/p : - g=3;
extern int g=10; void abc() { ++g; } void main() { ++g; abc(); printf("g=%d”,g); o/p: g=12
extern int g=5; void abc() { ++g; } void main() { ++g; abc(0; printf(“g=%d”,g); } int g=10; o/p : error variable ‘g’ is initialized more than once
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.