KEMBAR78
Lecture - 8 Storage Class | PDF | Variable (Computer Science) | Scope (Computer Science)
0% found this document useful (0 votes)
22 views3 pages

Lecture - 8 Storage Class

The document explains storage classes in C programming, which define the visibility, location, and lifetime of variables. It covers four main types: automatic, extern, register, and static variables, detailing their scopes and behaviors. Examples illustrate how these storage classes function within code, emphasizing the differences between local and global variable accessibility.

Uploaded by

akash.inside69
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)
22 views3 pages

Lecture - 8 Storage Class

The document explains storage classes in C programming, which define the visibility, location, and lifetime of variables. It covers four main types: automatic, extern, register, and static variables, detailing their scopes and behaviors. Examples illustrate how these storage classes function within code, emphasizing the differences between local and global variable accessibility.

Uploaded by

akash.inside69
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/ 3

Storage Class

A storage class represents the visibility and a location of a variable. It tells from what part of
code we can access a variable. A storage class is used to describe the following things:

• The variable scope.


• The location where the variable will be stored.
• The initialized value of a variable.
• A lifetime of a variable.
• Who can access a variable?

Scope i.e where the value of the variable would be available inside a program.
Default initial value i.e if we do not explicitly initialize that variable, what will be its default
initial value.
Lifetime of that variable i.e for how long wills that variable exist.
The following storage classes are most often used in C programming, actually the types of the
storage class are:

1. Automatic variables
2. External variables
3. Static variables
4. Register variables
Local Variable (Automatic)
The variables defined using auto storage class is called as local variables. Auto stands for
automatic storage class. A variable is in auto storage class by default if it is not explicitly
specified.
The scope of an auto variable is limited with the particular block only. Once the control goes
out of the block, the access is destroyed. This means only the block in which the auto variable is
declared can access it.
A keyword auto is used to define an auto storage class. By default, an auto variable contains a
garbage value.

#include <stdio.h> When you run the above program, you will get an
int main(void) error undeclared identifier i. It's because i is
{ for (int i = 0; i < 5; ++i) declared inside the for loop block. Outside of the
{ block, it's undeclared.
printf("C programming");
}
printf("%d", i);
return 0;
}
Extern storage class (Global Variables)
Extern stands for external storage class. Extern storage class is used when we have global
functions or variables which are shared between two or more files.

Keyword extern is used to declaring a global variable or function in another file to provide the
reference of variable or function which have been already defined in the original file.

The variables defined using an extern keyword is called as global variables. These variables are
accessible throughout the program. Notice that the extern variable cannot be initialized it has
already been defined in the original file.

#include <stdio.h> value of the external integer is = 48


int i;
main()
{
i=48;
printf("value of the external integer is = %d\n", i);
return 0;
}

Register Variable

The register keyword is used to declare register variables. Register variables were supposed to
be faster than local variables.
It is similar to the auto storage class. The variable is limited to the particular block. The only
difference is that the variables declared using register storage class are stored inside CPU
registers instead of a memory. Register has faster access than that of the main memory.

****Even though we have declared the storage class of our variable number as register, we
cannot surely say that the value of the variable would be stored in a register. This is because
the number of registers in a CPU is limited. Also, CPU registers are meant to do a lot of
important work. Thus, sometimes they may not be free. In such scenario, the variable works as
if its storage class is auto.

Static storage class

The static variables are used within function/ file as local static variables. They can also be used
as a global variable.
Static local variable is a local variable that retains and stores its value between function calls or
block and remains visible only to the function or block in which it is defined. Static global
variables are global variables visible only to the file in which it is declared.
#include <stdio.h> 6 11
void display()
int main()
{
display();
display();
}
void display()
{
static int c = 1;
c += 5;
printf("%d ",c);
}

You might also like