×

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 get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

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.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

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.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.