Sunday, 24 December 2017

Storage Classes in C Language - Geeks4Coding


Storage Classes in C

A storage class defines the scope or visibility and life-time of variables and or a functions within a C Program. They prefix data type . There four different storage classes in a C program −
  • auto
  • register
  • static
  • extern

Auto Storage Class

The auto storage class is the default storage class for all local variables. And use of it does not change anything for normal local variable.

int main()
{
int mount;
auto int month;
}

The above defines two variables with in the same storage class. 'auto' can only be used within functions, i.e., local variables.

Register Storage Class

 The register storage class is used to store local variables on register instead of RAM. This means that the variable has a maximum size equal to the register size and can't use unary '&' operator to get its memory location.

{
register int miles;
}

The register should only be used for variables that require quick access such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.

Static Storage Class

Static variables have a property of preserving their value even after they are out of their scope. Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

Syntax:
static data_type var_name = var_value;

Following are some interesting facts about static variables in C.

  •   A static int variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over.
For example, we can use static int to count number of times a function is called, but an auto variable can’t be sued for this purpose.

For example below program prints “1 2”

#include<stdio.h>
int fun()
{
static int count = 0;
count++;
return count;
}

int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}

 

Output:
 

1 2
But below program prints 1 1

 

#include<stdio.h>
int fun()
{
int count = 0;
count++;
return count;
}

int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}

 

Output:
 

1 1

  • Static variables are allocated memory in data segment, not stack segment.
  • Static variables (like global variables) are initialized as 0 if not initialized explicitly. For example in the below program, value of x is printed as 0, while value of y is something garbage.

#include <stdio.h>

 int main()
{
static int x;
int y;
printf("%d \n %d", x, y);
}

 

Output:
 

0
[some_garbage_value]


  • In C, static variables can only be initialized using constant literals. For example, following program fails in compilation.
  #include <stdio.h>
int initializer(void)
{
return 50;
}

int main()
{
static int i = initializer();
printf(" value of i = %d", i);
getchar();
return 0;
}

 

Output
 

In function 'main':
error: initializer element is not constant
static int i = initializer();
^

Please note that this condition doesn’t hold in C++. So if you save the program as a C++ program, it would compile \and run fine.

  • Static global variables and functions are also possible in C/C++. The purpose of these is to limit scope of a variable or function to a file. Please refer Static functions in C for more details.

Extern Storage Class

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.
 

When you have multiple files and you define a global variable or function, which will also be used in other files, then extern will be used in another file to provide the reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.
 

Please refer Extern keyword for more details

No comments:

Post a Comment

Thank you for your Time. Keep Learning

Geeks4Coding

Contributors

Popular Posts