×

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and functions. These features help the developers or programmers trace the existence of a particular variable during the program’s runtime.

They also precede the type that they modify. It also represents the location of variables and functions. It tells the compiler from which part of the code we can access the variable or function.

Types of Storage class in C

There are four types of storage class in C:

  1. Auto
  2. Extern
  3. Static
  4. Register
Storage specifierStorageScopeInitial valueLife
AutoStackWithin the blockGarbageEnd of the block
ExternData segmentGlobalZeroUntil the end of a program
StaticData segmentWithin the blockZeroUntil the end of a program
RegisterCPU RegisterWithin the blockGarbageEnd of the block
  1. Auto
  • The variables that are used to define the auto storage class are also known as local variables.
  • Auto means automatic storage class. A variable is present in the auto storage class by default if it is not specified explicitly.
  • The scope of this variable is limited within the particular block. Once the control goes out of the block, the access will be destroyed; that is, the block in which the auto is declared can access it.
  • By default, it consists of garbage value.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 int main()
 {
 auto int j = 1;
 {
 auto int j = 2;
 {
 auto int j = 3;
 printf (“ The values are: %d \t”, j);
 }
 printf (“ %d \t”, j);
 }
 printf (“ %d \t”, j);
 } 

Output

Storage class in C
  • Extern
  • The extern class is used to give reference of a global variable, which is visible to all the program files.
  • When extern is used, the variable cannot be initialized. However, it points the variable name at a storage location that has been defined previously.
  • When there are multiple files, you can define a global variable or a function, which can be used in other files, then the extern will be used in another file to provide the reference of a defined function or variable.
  • Extern is used to define a global variable or a function in other files.
  • Extern is most commonly used when there are two or more files sharing the same global variable or function.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 int a;
 int main()
 {
 extern int a;
 printf (“The value of a is: %d \n”, a); //default value is a
 return 0;
 } 

Output

Storage class in C

Note: In extern storage class, if a variable is declared as an external, then the compiler searches for that variable to be initialized somewhere in the program that may be static or extern. If it is not, then the compiler will throw an error.

  • Static
  • It is the type of storage class that is used to declare static variable which are used while writing programs in C.
    • Static variables have a property of storing the value even after they are out of the scope.
    • Hence, they preserve the value of their last use in their scope. It instructs the compiler to keep a local variable in existence during the life time of a program instead of creating and destroying it every time it comes in and goes out of scope.
  • Later, the static makes local variables to maintain their values between function calls. No new memory will be allocated since they are not redeclared. Their scope is local to the function or variable to which they were defined.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 void function(void);
 static int count = 5;
 int main()
 {
 while (count--)
 {
 func();
 }
 return 0;
 }
 void func(void)
 {
 static int i = 5;
 i++;
 printf (“i is %d and count is %d \n”, i, count);
 } 

Output

Storage class in C
  • Register
    • Register is used to declare register variables. They are supposed to be faster than the local variables.
    • However, present data compilers are good at optimization of code and there will be rare chances of using the register variable that will make the program much faster.
    • Register storage class is used to define local variables that are stored in register instead of the RAM, that is, the variable must have maximum size equal to that of register size and cannot have any unary operator applied to it.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 int main()
 {
 register int a;
 printf (“The value of a is: %d \n”, a);
 return 0;
 } 

Output

Storage class in C

Related Topics

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

Doubly Linked list in C

To know the Doubly Linked List in C, first we should know about how the Linked List works. Linked List The Linked list is the linear data structure. In the Linked...

5 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.