What are the local variable and global variable?

local variable and globle variable
What are the local variable and globle variable?


Local Variable
The variable that are declared inside the function are known are local variable the variable declared in the header of the function definition are also treated as local variable of that function. The local variables can only be used in the function in which they are declared.

Local variables are also called the automatic variable. The keyword auto can be used to declare these variable. However, the use of auto is optional.

Scope of local variable
The portion of a program where a variable is used (accessed) is know as scope. A Local variable declared in a function can only be used in that function only.

Lifetime of local variable
The time period for which a variable exists in memory is called lifetime of variable. Its lifetime starts when its is created in memory and ends when it is destroyed from memory. Avariable can only be used during its lifetime.

The time period for which a local variable exists in memory is called lifetime of local variable. The local variable are created when control enters into function. They are destroyed from memory when control returns back to calling function. When same function is called again, local variable are created again.

Global Variables
The variable that are declared outside the main function or any other function are called global variables or external variable. They hold heir values during the entire execution of program.

The value of global variable can be shared among different functions. If any function changes the value of a global variable, the modified value is also available to other function

        The use of global variables is not recommended, because:
v They are accessible to all functions and it becomes difficult to track changes In their values.
v They occupy a large amount of memory permanently during program execution and data accessing speed of program amy become slow.

·        Scope of Global Variable
A global variable can be used by all functions in the programs. It has a file scope.

·        Lifetime of global variable
The time period for which a global variable exists in memory is called lifetime of a global variable. The lifetime of global variable is between the starting and the terminating of a program execution. These variables exist in the memory throughout the program execution.

Comparison between Local and Global Variables
Local Variable
Global Variable
They are declared inside the main() function or any other user-define function
They are declared outside the main() function or any other user-define function
They can be used or accessed in the function in which they are declared.
They can be used or accessed in any function of the program.
They are created into memory when control enter in to the function or block in which they are destroyed from memory when control exits the function or block
Usually, they are created into memory when are destroyed form memory when program execution is terminated.
The values of local variables cannot be shared among other functions.
The values of global variables can be shared among other functions.
The use of local variables is recommended during program designing
The use of global variables is not recommended during program designing.


What are the local variable and global variable? What are the local variable and global variable? Reviewed by JD Ahmad on June 12, 2018 Rating: 5

No comments:

Powered by Blogger.