Local variables in C

Local variables are variables declared within a function or more specifically say within a block.

Block is a sequence of statements grouped together inside a pair of curly braces { and }. Since the first day of programming, you have been using blocks. For example – if…else block, loop block, function block etc.

Properties of a local variable

  1. A local variable is allocated on C stack.
  2. Local variables are uninitialized by default and contains garbage value.
  3. Lifetime of a local variable is until the function or block. A local variable dies once the program control reaches outside its block.
  4. Local variable is accessed using block scope access.

Block scope access

Block scope states that, variables declared inside a block can be accessed only within same or inner blocks. Block inside a block is called as inner or nested block.

Following are various accessibility rules of a variable defined by block scope.

  1. You can access a variable only within same or its inner block. For example –
    #include <stdio.h>
    
    int main()
    {
        int outervariable = 10;
    
        {
            printf("I can access outervariable in inner block = %d\n", outervariable);
        }
    
        printf("I can also access outervariable in same block = %d", outervariable);
    
        return 0;
    }

    Output of the above program.

    I can access outervariable in inner block = 10
    I can also access outervariable in same block = 10
  2. You can declare two or more variables with same name in different blocks. However, you cannot declare more than one variable of same name within same block. For example –
    int main()
    {
        int num = 10; 
    
        // I cannot declare num again in same block
        // int num;
    
        {
            // I can declare num again in different block
            int num = 20;
        }
    
        return 0;
    }

    In the above code I have commented the re-declaration of int num. As the program won’t compile if you remove comments.

  3. Inner block shadows outer block variable if both declared with same name. If both inner and outer block variable is declared with same name, then you cannot access outer block variable with same name inside inner block. For example –
    #include <stdio.h>
    
    int main()
    {
        int num = 10; 
    
        // Accesses outer block num
        printf("'num' is accessible within same block = %d\n", num);
    
        {
            // Declare num again but in different block.
            int num = 20;
    
            // Access inner block num
            printf("Inner block 'num' shadows outer block 'num' = %d\n", num);
        }
    
        return 0;
    }

    Output of the above program.

    'num' is accessible within same block = 10
    Inner block 'num' shadows outer block 'num' = 20
  4. Variables declared within inner block are not accessible to outer block. For example –
    #include <stdio.h>
    
    int main()
    {
        int num1 = 10;
        int num2 = 20; 
    
        printf("'num1=%d' and 'num2=%d' declared in outer block is accessible within same block.\n", num1, num2);
    
        {
            // num1 and num2 are accessible within inner block
            int sum = num1 + num2;
    
            printf("sum = %d\n", sum);
        }
    
        // sum is declared inside inner block hence is not accessible to outer block
        // printf("sum = %d\n", sum);
    
        return 0;
    }

    In the above program sum is declared inside inner block and can’t be accessed outside its block.

Local variables best practices

You have been using local variables since the first day of programming in C. However, always follow these best practices to avoid errors in your program.

  • Always try to minimize the usage of variables with same name within outer and inner block to avoid ambiguity. In addition, variables declared with same name within outer and inner blocks are complex to read and trace errors.
  • It is a good programming practice to initialize local variables before use to override its garbage value.