Jun 12, 2016

C Programming #66: Storage Class

C Programming #66: Storage Class

This article tries to cover the different storage class used in C. Different storage class available in C are

  1. Auto
  2. Extern
  3. Static
  4. Register

Most often used storage class are auto, extern and static. Register is least used.


Auto

Auto variables can be defined using auto keyword. Most of variables defined inside the function are auto variables. Till now we never used the auto keyword, since for variable defined inside the function is by default auto. Lets take a small C program to demo it.

#include <stdio.h>
int main() 
{
   auto int a = 10; // a is automatic variable
   int b = 20;      // b also is automatic variable

   printf("Auto variable a and b is %d and %d\n", a, b);

   return 0;
}
Auto variable a and b is 10 and 20

Note that auto variable a and b can be used only in function main, they cannot be used outside it.

Extern

This is already touched upon in this article - Difference between declaration and definition; please do revisit before proceeding further. Global variables which are defined outside any function have global scope, they can be referenced inside all the function, in same file or different file. Variable is made extern by keyword extern before its declaration. Lets see how a variable in one file is accessed in another file using extern.

  • a.c
#include <stdio.h>

extern int glob;

int main()
{
   printf("Value of glob is %d\n", glob);

   return 0;
}
  • g.c
int glob = 1000;

Now compiling above to files.

gcc a.c g.c -o glob
$ ./glob
Value of glob is 1000

Static

There are three types in which static storage class can be used.

File static variable

To prevent global extern access which was shown in previous example, we could restrict using file static variable. Lets modify f.c in above example to look like this.

#include <stdio.h>

static int glob = 1000;

void print_glob(void)
{
  printf("Value of glob is %d\n", glob);
}

Now compiling a.c and f.c will result in compiler error, because glob is restricted to f.c, now modifying a.c as below

#include <stdio.h>

extern void print_glob(void);

int main()
{
  print_glob();
  return 0;
}

Hence here we restricted the access of glob to file and gave a function print_glob, by which other parts of program could use it. Hence static global variables are good way of restricting the access of variables. This comes in very handy when writing and maintaining a very large C program.

Function static variable

Let me explain this concept by taking a small C problem - Write a C program to count number of times C function is called without using global variable. Let us dive into by writing it

#include <stdio.h>
void function();
int main()
{
   function();
   function();
   function();
   return 0;
}
void function()
{
   int count = 0;
   count++;
   printf("Function is called %d times\n", count);
   return;
}
Function is called 1 times
Function is called 1 times
Function is called 1 times

It is clear from the output that it is not working. Here count is auto variable, which is created when the function is called initialized to 0 incremented and when the function is over it is destroyed from stack, and next time when it is called same thing repeats. To make it retain value between the function call, we need to make it as static variable. The modified code would look as follows.

#include <stdio.h>
void function();
int main()
{
   function();
   function();
   function();
   return 0;
}
void function()
{
   static int count = 0;
   count++;
   printf("Function is called %d times\n", count);
   return;
}
Function is called 1 times
Function is called 2 times
Function is called 3 times

Here static makes compiler to retain the value between the function call.

File static function

Similar to file static variable, to restrict the function to a file, we use file static function. This way we could restrict few of the internal function from being called from other files. It is like making function private to that file. This is also very useful when maintaining very large C Program.

Register

Variable can be made to stored in CPU register by declaring them as register. Since the register access is faster than memory access Program performance will be faster.

#include <stdio.h>
int main()
{
   register int a = 10;
   printf("Value of register variable a is %d\n", a);
   return 0;
}
Value of register variable a is 10

Note register keyword is just a suggestion to compiler to store the variable in register and compiler can easily ignore. With modern compiler optimization, most of the user should detest from using it and leave the performance optimization to compiler.

No comments :

Post a Comment