C – Storage Class and Nested Loop

Storage Class and Nested Loop

‘Storage’ refers to the scope of a variable and memory allocated by the compiler to store that variable.

The scope of a variable is the boundary within which a variable can be used. Storage class defines the scope and lifetime of a variable.

From the point view of C compiler, a variable name identifies the physical location from a computer where the variable is stored.

There are two memory locations in a computer system where variables are stored as Memory and CPU Registers.

Functions of Storage Class

  • To determine the location of a variable where it is stored?
  • Set the initial value of a variable or if not specified then setting it to the default value.
  • Defining the scope of a variable.
  • To determine the life of a variable.

There is four types of storage classes are available in C programming language.

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

Automatic Storage Class

A keyword, the auto is used to specify an automatic variable. But this is the default. Variables defined within a function are automatic anyway.

These variables are also known as local variables. The local variables don’t have the meaning outside of the function body. 

{
           int a;
          char b;
          auto float c;
}

Register Storage Class

The ‘register’ keyword is used to represent the register storage class in C programming. The variable declared as the register is stored in the CPU register.

The default value of that variable is a garbage value. Scope of that variable is local to the block in which the variable is defined. Variable is alive till the control remains within the block in which the variable id defined.

The main difference between auto and register is that variable declared as the auto is stored in memory whereas the variable declared as the register is stored in a CPU register.

Since the variable is stored in a CPU register, it takes very less time to access that variable. Hence it becomes very time efficient. It is not necessary that the variable declared as register would be stored in CPU registers.

The number of CPU registers is limited. If the CPU register is busy doing some other task then variable might act as an automatic variable.

#include<stdio.h>
void main(){
    register int a;
    printf(“%d”,a)
}

External Storage Class

The external variables are defined outside of (external to) any function. The external variables are visible to all the functions in a program. These variables also called as global variables.

The Extern keyword is used before a variable to inform the compiler that this variable is declared somewhere else. The external declaration does not allocate storage for variables

#include<stdio.h>
void C();
int b=5;    
int main(){
    b+=4;
    C();
    return 0;
}
void C(){
   ++b;
   printf("a=%dn",a);
}

C Output

10

Static Storage Class

The ‘static’ keyword is used to represent static storage class in C programming. The variable declared as static is stored in the memory. The default value of that variable is zero.

The scope of that variable is local to the block in which the variable is defined. Live of variable persists between different function calls.

#include<stdio.h>
void test();
main()
{
test();
test();
test();
}
void test()
{
static int a=0;
a=a+1;
printf("%dt",a);
}

C Output

1   2  3 

Nested Loop

When the body part of the loop is another loop then the inner loop is said to be nested within the outer loop.

Since a loop may contain another loop within its body, therefore there is no limit of a number of loops that can be nested. 

C Nested Loop Example 

for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
}
printf("%d",i);
}

C Code

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
}
printf("%d",i);
}
printf("n");
}
getch();
}

Result:
1
22
333
4444
55555