top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Storage Classes in C/C++

+13 votes
611 views

What is Storage Class in C/C++
A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program.

There are following storage classes which can be used in a C/C++ Program

auto storage class
auto is the default storage class for all local variables.

{
  int a;
  auto int b;
}

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

register storage class
Register behaves just like auto, except that instead of being allocated onto the stack, they are stored in a register.

Registers offer faster access than RAM, but because of the complexities of memory management, putting variables in registers does not guarantee a faster program—in fact, it may very well end up slowing down execution by taking up space on the register unnecessarily. As it were, using register is actually just a suggestion to the compiler to store the variable in the register; implementations may choose whether or not to honor this.

Example 
{
   register int  m;
}

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

static int C;
    int R;
    {
        printf("%d\n", R);
    }

static variables can be 'seen' within all functions in this source file. At link time, the static variables defined here will not be seen by the object modules that are brought in.

static can also be defined within a function. If this is done the variable is initalised at run time but is not reinitalized when the function is called. This inside a function static variable retains its value during vairous calls.

   static count=5;   
   main()
   {
     while (count--) 
     {
         func();
     }   
   }

   void func( void )
   {
     static i = 5;
     i++;
     printf("i is %d and count is %d\n", i, count);
   }

This will produce following result

   i is 6 and count is 4
   i is 7 and count is 3
   i is 8 and count is 2
   i is 9 and count is 1
   i is 10 and count is 0

extern storage class
extern 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 initalized as all it does is point 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 be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to decalre a global variable or function in another files.

File 1: main.c

   int count=5;

   main()
   {
     write_extern();
   }

File 2: write.c

   void write_extern(void);
   extern int count;

   void write_extern(void)
   {
     printf("count is %i\n", count);
   }

Here extern keyword is being used to declare count in another file.

Now compile these two files as follows

   gcc main.c write.c -o write

This fill produce write program which can be executed to produce result. Count in 'main.c' will have a value of 5. If main.c changes the value of count - write.c will see the new value

posted Dec 30, 2013 by anonymous

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

It is also called the ternary operator and used for inline if-else mostly for assignment operations.

SYNTAX

Condition ? expression 2 : expression 3
If Condition is true expression 1 is returned 
If Condition is false expression 2 is returned

Example
Say we have following C/C++ if-else statement

if (a > b) {
    result = x;
} else {
    result = y;
}

This can be rewritten as the following statement using the conditional or ternary operator.

result = a > b ? x : y;

Differences between C and C++ behavior

The conditional operator in C++ can return an lvalue, whereas C does not allow for similar functionality. Hence, the following is legal in C++:

(true ? a : b) = 1;

To replicate this in C, you would have to resort to if/else, or deal with references directly:

*(true ? &a : &b) = 1;
READ MORE
...