What is Storage Classes in C Language: Auto, Extern, Static, Register

0
748
what is C tokens

Storage classes in C language represent the location of a variable and it describes its features. We can know about A block of code can be accessed using a  particular variable with the help of storage classes. C storage classes are of four types. Let us take a look at them.

Storage Classes in C Language

Before we know about these storage classes in the C language, we should first understand some terms. These terms are:

A) Scope of a Variable in C

Scope in any programming language can be defined as a part or region of a code where a defined variable exists. The variable can only be accessed in that scope and cannot be accessed beyond it. Variables are divided into three types based on the part of the code they are declared in. These are:

i) Local Variable in C

These variables are the ones that are declared inside a particular function. That means you can use these variables only inside the function they belong to. For instance,

int fun()

{

int a, b, c;

scanf(“%d, %d”, &a, &b);

printf(“%+% = c = %d”, a, b, c);

}

ii) Global Variables

These are the variables that are declared outside any function. For instance,

int a, b;

int main()

{

}

Here, a and b are global variables.

iii) Formal Parameters

When variables are declared during the definition of a function, they are called Formal Parameters. For instance,

int add(int x, int y)

{

}

B) Life of a Variable

Life/ Lifetime of a variable is the duration during which memory is allocated to a variable. In simple words, it is the time during which a variable is stored in memory.

C) Garbage Value

Whenever we declare a variable in C, some memory blocks are assigned to it. For instance,

we declare a variable of x with data type integer, int x. Here, some memory has been named x and has been assigned. But we have not yet initialized x.

That means, we have not assigned an initial value to x. So, the block of memory that has been assigned to x still contains some waste value from previous programs and files. This waste value is called the garbage value. To get rid of garbage value, the initialization should be done. For instance,  int x = 0

How to implement Sizeof and Strlen function in C?

D) Block of Code

A block of code is nothing but a few lines of code that lie within the curly braces. For instance,

if (x>0)

{

printf(“x is smaller than 0.”);

printf(“x is unusable.”);

}

The above example is a block of code.

What is the string in C programming?

Storage Class Syntax 

storage_class_name data_type_of_variable variable_name;

Different Storage Classes in C Language

Now that you have understood these two crucial terms, let us talk about the various storage classes available in C.

Storage Class Specifiers in C Storage Type Initial Value Assigned Scope of the variable Lifetime
Auto Stack Garbage Value Inside the block Till the end of the block
Extern Data Segment 0 Global Till the end of the code
Static Data Segment 0 Inside the block Till the end of the code
Register Stored in the CPU register Garbage Value Inside the block Till the end of the block

Let us now talk about these storage classes in C language in detail.

I) AUTO Storage Class in C Language

All the variables that are declared inside a function have Auto as their default storage class. Use the “Auto” keyword to specify this. But since it is assigned by default, it is rarely used.

Since the scope of Auto is limited to a block, variables with Auto can only be accessed from inside the block. If someone wants to access them outside the block, they will have to use Pointers. For instance,

#include <stdio.h>
int main()
{
auto int x=5;
{
auto int x=6;
{
auto int x=8;
printf ( “%d\n”,x);
}
printf ( “%d\n”,x);
}
printf ( “%d\n”,x);
return 0;
}

Output

8

6

5

II) EXTERN Storage Class in C Language

Whenever a variable is defined somewhere else but is used in a block or a function, the extern keyword comes into play.

What is Pointer & How many types of Pointers available in C Programming?

Extern variables are those variables that are declared in one place but are then used at various other places. So their lifetime is till the end of the program because they can be used globally.

To convert a normal variable into an extern variable, write an extern keyword before it. For instance,

let us create two different files. Let us name the first file – new1.c and the second file – new2.c. new1.c contains the following code:

#include <stdio.h>
x=100
//new2.c contains the following code:
#include <stdio.h>
extern x;
main()
{
printf (“Value is = %d\n”, x);
return 0;
}

Output

Value is = 100

III) STATIC Storage Class in C Language

This is the most used keyword for storage classes in the C language. Static is used because they can deserve their values even after their scope has ended. That means that after they have been initialized once, they can be used till the end of the program.

So they have been stored in the memory once and new memory is not needed every time. If static variables are declared globally, they can be accessed anywhere and anytime in the program. For instance,

#include <stdio.h>
void stat_fun(void);
static int n=5;
int main()
{
while(n–)
{
stat_fun();
}
return 0;
}

void stat_fun(void)
{
static int x=5;
x++;
printf (“x %d and n %d\n”, x, n);
}

IV) REGISTER Storage Class in C Language

Register variables are somewhat similar to Auto variables when it comes to functionality. The difference between the two is that register variables are stored in a different storage type as compared to Auto variables.

How many Directives in C language?

While Auto variables are stored in the stack, register variables are stored inside a microprocessor register.

When a register is empty i So the register variables can be accessed faster in comparison to other variables. Register variables are stored in memory only if the register is not available. For instance,

#include <stdio.h>
int main()
{
{
register int x;
int *ptr=&x;
}
return 0;
}

Output

We got this error because we did not get a memory location.

Frequently Asked Questions From Storage Classes in C Language

Q1. Is it Bad to overuse the static storage classes Specifier?

Ans. Static is a very useful keyword. It can make a lot of things easier. But overusing the static keyword in C might come with some side effects.

The only thing that one should remember while using static is that he should know why exactly he is using it and how will it help his program. Also, one should keep a track of the number of static keywords he has used in his program.

How to implement Sizeof and strlen in C?

Q2. How to use storage classes in C programming?

Ans. You can use storage classes in any programming language by writing keywords against the name of the variables. Each storage class has a keyword.

For instance,  To use static storage class, the syntax would be – static data_type var_name;

Q3. Typedef is a storage class or not?

Ans. In c, consider the typedef as a storage class specifier. Although the typedef does not deal with the storage type of variables, its syntax is similar to those of other storage classes.

What is Strings in C Programming?

Hence, most people consider it as a storage class specifier. In effect, the typedef is somewhat similar to #define.

Q4. What are Default Storage Classes in C language?

Ans. In c, the default storage class is Auto. No need to use an auto keyword in C against a variable, the storage class becomes auto on its own.

Q5. A real-life example of static Storage class in C?

Ans. As we have already seen, the static storage class has its lifetime until the end of the program that makes it the most widely used storage class because of its extensive application.

What is the role of Operator precedence in C programming?

Static can be used whenever a variable has to be declared locally but its value has to be retained and has to be used in a lot of places in the program.

Q6. What is the difference between static and extern?

Ans. When a variable’s value has to be retained until the end of the program while being declared locally.

Extern in C is used when a variable has been declared globally and it can be used anywhere and anytime in a program, and it retains its value unless it has been changed manually.

Q7. When to Use Storage Classes in C Language?
Ans. Each storage class has a different use and you can use it according to your needs.

a) You can use the Static keyword in C only when you want the value of a variable to retain throughout different function calls.

b) If you want to use a variable again and again in a program then you should use the register keyword in C. There are very few registers available in a CPU. So, use it very carefully.

How many types of operators in C?

c) Declare extern keyword in C globally, and use extern for those variables that need to be used throughout the program and in various functions.

d) Since the auto in c is a default storage class, you do have to write the keyword. Also, if you do not have the above-mentioned needs from your variables, then only the use of Auto is required.

Sample Programs with Output
1.
#include <stdio.h>
void main()
{
int i;
for(i=0; i<=50000; i++)
printf(“\n%d”, i);
}

Output

0 to 49999 all digits display on the output screen

2.
#include <stdio.h>
void main()
{
func();
func();
}
void func()
{
auto int i=0;
register int j=0;
static int k=0;
i++;
j++;
k++;
printf(“\n%d %d %d\n”, i, j, k);
}

Output

1 1 1

1 1 2

I hope you have understood what storage classes are in the C language. If you have any queries related to it. Please comment on us.

LEAVE A REPLY

Please enter your comment!
Please enter your name here