0% found this document useful (0 votes)
23 views21 pages

4.3storage Class

A storage class defines the scope and lifetime of variables and functions in C. The main storage classes are auto, register, static, and extern. Auto variables are allocated on the stack and freed when the block ends. Static variables retain their value between function calls. Extern provides references to global variables across files.

Uploaded by

aaryankamdar2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views21 pages

4.3storage Class

A storage class defines the scope and lifetime of variables and functions in C. The main storage classes are auto, register, static, and extern. Auto variables are allocated on the stack and freed when the block ends. Static variables retain their value between function calls. Extern provides references to global variables across files.

Uploaded by

aaryankamdar2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Storage class

• A storage class defines the scope (visibility) and life-time of variables and/or
functions within a C Program.
• auto
• register
• static
• extern
Automatic

• Automatic variables are allocated memory automatically at runtime.


• The automatic variables are initialized to garbage by default.
• The memory assigned to automatic variables gets freed upon exiting from the block.
• The keyword used for defining automatic variables is auto.
• Every local variable is automatic in C by default.
• The variables declared inside a block are automatic or local variables.
• The local variables exist only inside the block in which it is declared.
#include <stdio.h>
int main()
{
int a; //auto
char b;
float c;
printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c.
return 0;
}
Output:
garbage garbage garbage
#include <stdio.h>
int main()
{
int a = 10,i;
printf("%d ",++a); //11
{
int a = 20;
for (i=0;i<3;i++)
{
printf("%d ",a); // 20 will be printed 3 times since it is the local value of a
}
}
printf("%d ",a); // 11 will be printed since the scope of a = 20 is ended.
}
• #include<stdio.h>
• main ( ){
• auto int i=1;{
• auto int i=2;{
• auto int i=3;
• printf ("%d",i)
• }
• printf("%d", i);
• }
• printf("%d", i);
• }
• Static
• The variables defined as static specifier can hold their value between the multiple function calls.
• Static local variables are visible only to the function or the block in which they are defined.
• A same static variable can be declared many times but can be assigned at only one time.
• Default initial value of the static integral variable is 0 otherwise null.
• The visibility of the static global variable is limited to the file in which it has declared.
• The keyword used to define static variable is static.
#include<stdio.h>
static char c;
static int i;
static float f;
static char s[100];
void main ()
{
printf("%d %d %f %s",c,i,f); // the initial default value of c, i, and f will be printed.
}
#include <stdio.h>
// function with static variable

int fun()
{

static int count = 0;


count++;

return count;
}

int main()
{
printf("%d ", fun());
printf("%d ", fun());

return 0;
}
Output: 1 2.
static variables are only initialized once and live till the end of the program. That is why they can retain their value between multiple function calls.
extern Storage Class

• extern storage class is used to give a reference of a global variable


that is visible to ALL the program files.
• When you have multiple files and you define a global variable or
function, which will also be used in other files, then extern will be
used in another file to provide the reference of defined variable or
function.
#include <stdio.h>
int count;
extern void write_extern();
main()
{
count = 5;
write_extern();
}
#include <stdio.h>
extern int count;
void write_extern(void)
{
printf("count is %d\n", count);
}
#include <stdio.h>
extern i;
main()
{ printf("value of the external integer is = %d\n", i);
return 0;}

#include <stdio.h>
i=48;
#include <stdio.h>
void display ()
{ extern int x;
printf("%d\t",x);}
int main()
{ extern int x;
printf("%d\t",x);
display();
return 0;
}
int x=5;
Register Variable

• The register keyword is used to declare register variables. Register variables were supposed to be
faster than local variables.
• However, modern compilers are very good at code optimization, and there is a rare chance that using
register variables will make your program faster.
• Unless you are working on embedded systems where you know how to optimize code for the given
application, there is no use of register variables.
• The variables defined as the register is allocated the memory into the CPU registers depending upon the size of
the memory remaining in the CPU.
• The access time of the register variables is faster than the automatic variables.
• The initial default value of the register local variables is 0.
#include <stdio.h>
int main()
{
register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0.
printf("%d",a);
}
#include <stdio.h>
int main()
{
register int a = 0;
printf("%u",&a); // This will give a compile time error since we can not access the address of a register variable.
}
#include <stdio.h>
int main()
{
register int a = 10;
++a;
printf("Value of a : %d", a);
printf("\nEnter a value");
a=30;
--a;
printf("\n Value of a : %d", a);
return 0;
}
• Output:
Value of a : 11
Enter a value 30
Value of a : 29
Thank You

You might also like