Skip to main content

Storage Classes in C programming

C has a concept of 'Storage classes' which are used to define the scope (visibility) and a lifetime of variables and functions.And storage class determines how long it stays in existence.In c-programming, 4 type of storage class is there.



So what Storage Classes are available?
1.auto
2.static
3.extern
4.register

1.Auto - storage class

Auto is the default storage class for local variables.
        {
            int Count;
            auto int Month;
        }
The example above defines two variables with the same storage class. auto can only be used within (inside) functions, i.e. local variables.Local variables is stored in stack memory.


2.static - Storage Class

Notes:
For some reason, static has different meanings in different contexts.
When specified on a function declaration, it makes the function local to the file.
When specified with a variable inside a function, it allows the variable to retain its value between calls to the function. See static variables.
It seems a little strange that the same keyword has such different meanings....


   static variables
Static is the default storage class for global variables(not a function). The two variables below (count and road) both have a static storage class.

  #include<stdio.h>
     static int Count;
     int Road;
     main()
     {
       printf("%d\n", Count);
       printf("%d\n", Road);
     }

'static' can also be defined within a function. If this is done, the variable is initialized at compilation time and retains its value between calls. Because it is initialled at compilation time, the initialization value must be a constant. This is serious stuff - tread with care.

     void Func(void)
     {
       static Count=1;
     }
xample
There is one very important use for 'static'. Consider this bit of code.
#include <stdio.h>
void foo()
{    int a = 10;
     static int sa = 10;
      a += 5;
      sa += 5;
    printf("a = %d, sa = %d\n", a, sa);
}
int main()
{
    int i;
    for (i = 0; i < 10; ++i)
    foo();
}

static functions
  Static functions are functions that are only visible to other functions in the same file. Consider the following code.
main.c   (main c file)
#include <stdio.h>
   main()
   {
     Func1(); 
      Func2();
   }

funcs.c (funcs.c is the function file)
 /*************************************
    *
    * Function declarations (prototypes).
    *
    *************************************/
   /* Func1 is only visable to functions in this file. */ 
   static void Func1(void);
   /* Func2 is visable to all functions. */
   void Func2(void);
   /*************************************
    *
    * Function definitions
    *
    ************************************/
   void Func1(void)
   {
     puts("Func1 called");
   }
   /*************************************/
   void Func2(void)      
   {
     puts("Func2 called");
   }



3.extern - Storage Class

Extern defines a global variable that is visible to ALL object modules. When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.

        Source 1                                                     Source 2
        --------                                                             --------


        extern int count;                                          int count=5;

        write()                                                          main()
        {                                                               {
          printf("count is %d\n", count);                    write();
        }                                                                }
Count in 'source 1' will have a value of 5. If source 1 changes the value of count - source 2 will see the new value. Here are some example source files.




4.Register- storage class

The register is used to defining local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).
        {
          register int  Miles;
        }
The register should only be used for variables that require quick (fast) access - such as counters. It should also be noted that defining 'register' goes 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.

Comments

Popular posts from this blog

CAN INTERVIEW QUESTIONS

Qualifiers and Modifier in C

RTOS Real time operating system interview questions

CAN INTERVIEW QUESTIONS 2

What is UDS protocol

Memory mapping in c

TOP IOT PLATFORMS