Storage Classes in C and it's types

Storage classes in C






















What is the scope of the variable?

The scope of variable means that where it is available in the program.

See Also: Pointers in C

What is the lifetime of the variable?

Lifetime means how long a variable will stay in the memory. The lifetime of the variable also depends on the scope of the variable.

What is the initial default value of the variable?

The initial default value means that when we do not initialize a variable, what value does it store.



Types of the variable in C

We try to understand the types of variables with the help of a programming example. 
#include <stdio.h>
int d;
int main( )
{
 int add, a=10,b =20;
 add = a+b;
 printf ("Output of addition = %d\n",add);
 return 0;
}


See Also: Operators in C

1. Global variable

The variables that we declare before the main function or any function; we call them global variable.  Global variable stays in the memory throughout the program. For example, In the program, the variable 'd' is a global variable. The scope of the global variable is the entire program. We can use the variable 'd' anywhere in the program.

#include <stdio.h>
void newfunction( );
int d = 11;
int main ( )
{
    printf ("value of d = %d\n", d);
    newfunction ( );
    printf ("new value of d = %d\n",d);
    return 0;
}
 void newfunction ( )
 {
     d++;
 }

With the help of the program, we want to let you know that if we declare a variable, we can use it anywhere or in any function of the program.


Output

Storage Classes in C




When we declared variable globally then the default initial value is considered as zero.
#include <stdio.h>
int d;
int main( )
{
    printf("value of d= %d\n", d);
    return 0;
}

Output
value of d = 0

2. Local variable

And the variables that we have declared inside the function are called local variables. The scope of the variable is limited to function or block. For example, In the above program, the variable 'add,' 'a' and 'b' is the local variable. 

#include <stdio.h>
void newfunction( );
int d = 11;
int main ( )
{
    int c = 20;
    printf ("\n\n");
    printf (" value of d = %d\n", d);
    newfunction ( );
    printf ("  value of d and c= %d%d\n",d,c);
    return 0;
}
 void newfunction ( )
 {
     c++;
     d++;
 }

Output
Storage Classes in C




Because of the variable 'c' is declared inside the main function and not declared inside the 'newfunction' then there is an error in the program because 'c' is the local variable.


Types of Storage Classes in C




1. Automatic (auto) Storage Classes in C

Those variables defined inside the function or block is by default considered as a storage class in C. These variables are automatically finished when we come out of the function or block. The keyword 'auto' can be used to declare an automatic variable. These variables are stored in the stack memory.

Scope: These variables are local to the function or block.
Lifetime: Variables vanish when coming out of the function or block.
Default initial value: Garbage value.


Example
#include <stdio.h>
int main( )
{
    int a;
    auto int b;
    return 0;
}

In the above example, 'int a' and 'auto int b' are the same.

2. Static Storage Classes in C

If we declare a variable as static inside the function, then the variable remains until the program ends. If we declare a variable outside the function in a program, we can use this variable in the program not used in any other program.

Scope: These variables are local to the function or block.
Lifetime: This variable remains until the program's execution is complete.
Default initial value: Its initial value is considered as zero.

Example
#include <stdio.h>
void fun ( );
int main ( )
{
    fun ( );
    fun ( );
    fun ( );
    fun ( );
    return 0;
}
void fun ( )
{
    static int b = 0;
    b++;
    printf ("Static b %d\n", b);
}


Output
Storage Classes in C










3. Register Storage Classes in C

The register keyword suggests the compiler that the variable is stored in the CPU register, not in the memory. Accessing of a register variable is faster than the normal variable.


Scope: These variables are local to the function or block.
Default initial value: Garbage value
Lifetime: Till the end of function or block.

Example
#include <stdio.h>
int main ( )
{
    register int b = 11;
    printf ("address of b %u\n", &b);
    return 0;
}


Output
Storage Classes in C




We can't access the address of register's variable.

4. Extern Storage Classes in C

Let's understand the extern variable with the help of an example. In the below-given program, the variable 'a' has been declared outside the function, so let's see what the compiler tells us about this program.

#include <stdio.h>
int main ( )
{
    printf ("value of a %d\n",a);
    return 0;
}
int a = 23;

Output
Storage Classes in C




As we have seen, the compiler said that the variable 'a' had not been declared.

If we want to tell the compiler that the variable 'a' has been declared somewhere in the program, then we use 'extern' keyword.

Scope: Global that means everywhere in the program.
Default initial value: zero
Lifetime: Till the program doesn't finish its execution.



Storage Classes in C and it's types Storage Classes in C and it's types Reviewed by Glory on 09:07 Rating: 5

No comments:

Powered by Blogger.