Open In App

Local Variable in C

Improve
Improve
Like Article
Like
Save
Share
Report

In C language, a variable declared within a function or a block of code is called a local variable. Local variables are frequently used to temporarily store data in a defined scope where they can be accessed and manipulated. 

They are stored in the memory stack, Once the function or block of code in which the local variable is declared finishes executing. The variable is automatically removed from the memory.

Example of Local Variable

void gfg()
{
      int a=10;  // local variable declaration
}

Before a local variable can be used, it is necessary first to initialize the variable with a value.

Characteristics of Local Variable in C

There are certain important points about Local Variables as mentioned below :

1. Function Scoped Local Variables

The local variable is only accessible inside the function or block of code in which it is declared. A variable’s scope specifies where it can be used and accessible within a program. 

Below is the implementation of the above topic:

C




// C Program to demonstrate
// Function SCoped Local Variables
#include <stdio.h>
 
// Function declared
void gfg()
{
    // localVar is a local variable of
    // gfg function
    int localVar = 10;
 
    printf("The value of localVar is %d\n", localVar);
}
 
// Main Function
int main()
{
    gfg();
    return 0;
}


Output

The value of localVar is 10

Explanation: In the above code, When the function is called from the main function, it prints the value of localVar as 10. If you try to access it outside of its scope( in this example main function )will result in a compilation “error: ‘localVar’ undeclared”.  This is referred to as being out of scope.

2. Block Scoped Local Variables

Variables have a specified region or context in which they are valid and can be manipulated. After a variable goes beyond its scope, it can no longer be accessed or modified.

Below is the implementation of the above topic:

C




// C Program to demonstrate
// Block Scoped Local variables
#include <stdio.h>
 
// Driver Function
int main()
{
    // declaration of x variable outside the block
    int x = 15;
 
    // Block is defined here
    {
        // Block scoped variable "x"
        int x = 25;
        printf("x was declared in the block: %d \n", x);
    }
 
    // prints 15 that was declared outside
    printf("x outside the block: %d", x);
   
    return 0;
}


Output

x was declared in the block: 25 
x outside the block: 15

Explanation: Outside of the block, the program defines the variable “x” with a value of 15. Another variable, ‘x,’ is defined inside the block with a value of 25. The variable “x” inside the block is printed with a value of 25. We are unable to access the block-defined variable “x” outside of the block.

Advantages of Using Local Variables

  • Local variables are only usable within the scope in which they are declared, It avoids naming conflicts and maintains code organization.
  • Local variables are useful for storing temporary data that is only required within a particular function or block of code.
  • Local variables provide more security we can wrap the variables in a code block or function to limit access of those variables to that scope only.
  • Local variables can make the code easy to read.


Last Updated : 19 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads