Local Variables in C Programming With Example

Definition Of Local Variable

Local variables are the variables which are declared or defined within the declaration part of the function block.

These variables have local scope to that function only, in which they are declared.

They cannot be accessed outside of the function. The local variable exists until the block of the function is in under execution. After that, it will be destroyed automatically.

Function Block

  • Declaration part

    Region where we declare all variables which are going to be used within the function (this part starts from starting curly brace "{").

  • Executable part:

    Other statements except the declarations are the executable statements.

Example 1:



Output:

 Inside variable a = 20
 outside variable a = 10

The variable a created inside the compound statement or block i.e inside braces ({}) is completely different from variable a declared outside the block. As soon as the block ends the variable a declared inside the block is destroyed.

Example 2:



Output:

 a=100, b=200
x=10, y=20	

Read Also,