in , ,

C Storage Class

C Storage Class
C Storage Class

C Storage Class

In this tutorial, you will find out about the scope and lifetime of local and global variables. also, you will find out about static and register variables.

Every variable in C programming has two properties: type and storage class.

Type refers to the data type of a variable. And, storage class determines the scope, visibility, and lifetime of a variable.

There are 4 types of storage class:

1. automatic
2. external
3. static
4. register


Local Variable

The variable pronounced inside a square is programmed or nearby factors. The local variables exist just inside the square in which it is pronounced.

Let’s take an example.

#include <stdio.h>

int main(void) {
  
  for (int i = 0; i < 5; ++i) {
     printf("C programming");
  }
  
 // Error: i is not declared at this point
  printf("%d", i);  
  return 0;
}

When you run the above program, you will get an error undeclared identifier i. It’s because i is declared inside the for loop block. Outside of the block, it’s undeclared.

Let’s take another example.

int main() {
    int n1; // n1 is a local variable to main()
}

void func() {
   int n2;  // n2 is a local variable to func()
}

In the above example, n1 is local to main() and n2 is local to func().

This means you cannot access the n1 variable inside func() as it only exists inside the main(). Similarly, you cannot access the n2 variable inside the main() as it only exists inside func().


Global Variable

Factors that are pronounced outside of all capacities are known as outer or worldwide factors. They are available from any function inside the program.


Example 1: Global Variable

#include <stdio.h>
void display();

int n = 5;  // global variable

int main()
{
    ++n;     
    display();
    return 0;
}

void display()
{
    ++n;   
    printf("n = %d", n);
}

Output

n = 7

Assume, a global variable is proclaimed in file1. On the off chance that you attempt to utilize that variable in an alternate record file2, the compiler will grumble. To take care of this issue, the watchword extern is used in file2 to show that the outside factor is pronounced in another document.


Register Variable

The register catchphrase is used to proclaim register factors. Register factors should be quicker than nearby factors.

In any case, present-day compilers are excellent at code advancement, and there is an uncommon possibility that utilizing register factors will make your program quicker.

Except if you are chipping away at inserted frameworks where you realize how to streamline code for the given application, there is no use of register variables.


Static Variable

A static variable is declared by using the static keyword. For example;

static int i;

The value of a static variable persists until the end of the program.


Example 2: Static Variable

#include <stdio.h>
void display();

int main()
{
    display();
    display();
}
void display()
{
    static int c = 1;
    c += 5;
    printf("%d  ",c);
}

Output

6 11

During the first function call, the value of c is introduced to 1. Its worth is expanded by 5. Presently, the value of c is 6, which is imprinted on the screen.

During the subsequent function call, c isn’t introduced to 1 once more. This is on the grounds that c is a static variable. The worth c is expanded by 5. Presently, its value will be 11, which is printed on the screen.

Please feel free to give your comment if you face any difficulty here.


salman khan

Written by worldofitech

Leave a Reply

On-Page SEO Guide To Rank On The First Page

On-Page SEO Guide To Rank On The First Page

How to Recover Deleted Messages From Android Devices

How to Recover Deleted Messages From Android Devices