2. What is a Variable ?
A variable is a data name that may be used to store a data value. A
variable may take different values at different times of execution and may be
chosen by the user. it may consist of letters, digits and underscore character.
•Importance in programming
Variables are essential for managing data in a program. they enables
developers to store values of various data types, such as strings, integers and
floats, which can be used to perform calculations manipulate text, or control
program flow.
3. Scope of Variable
Each variable is defined and can be used within its scope and
determines that wherein the program this variable is available to
use. The scope means the lifetime of that variable. It means the
variable can only be accessed or visible within its scope.
The scope of variables can be defined with their declaration, and
variables are declared mainly in two ways.
4. Local Variable
Local variables are variables that are declared within a specific scope,
such as inside a function or a block of code.
Example-
#include<stdio.h>
Void main()
{
Int X=24, Y=4;
Printf(“X=%d and Y=%d”,X,Y);
}
5. Global Variable
Global variables are variables that are declared outside of any function
or block of code.
Example-
#include<conio.h>
Int A=10, B;
Void main()
{
Printf(‘A=%d and B=%d”,A,B);
}
7. It is declared inside a function. It is declared outside the function.
If it is not initialized, a garbage value
is stored.
If it is not initialized zero is stored as default.
It is created when the function starts
execution and lost when the functions
terminate.
It is created before the programs global execution starts
and lost when the program terminate.
Data sharing is not possible as data of
the local variable can be accessed by
only one function.
Data sharing is possible as multiple functions can access
the same global variable.
When the value of the local variable is
modified in one function, the changes
are not visible in another fuction.
When the value of the global variable is modified in one
function, the changes are visible in rest of the program.
Local variables can be accessed inside
a function in which they are declared.
you can access global variables be any statement in the
program.
8. WHEN TO USE LOCAL OR GLOBAL VARIABLE -
If you feel that a certain variable will take multiple
values by passing through various functions then use local
variables . If you feel that a certain variable you need to
use will have constant value, then declare it as a global
variable.
9. Advantages Of Local Variable:-
The same name of a local variable can be used in different functions as it is
only recognized by the function in which it is declared. Local variables use
memory only for the limited time when the function is executed, after that
same memory location can be reused
Disadvantages Of Local Variable:-
The scope of the local variable is limited to its function only
and cannot be used by other functions. Data sharing by the
local variable is not allowed.
10. Advantages Of Global Variable:-
Global variables can be accessed by all the functions present in the
program. Only a single declaration is required. Very useful if all the
functions are accessing the same data.
Disadvantages Of Global Variable:-
The value of a global variable can be changed accidently as it can
be used by any function in the program. If we use a large number of
global variables, then there is a high chance of error generation in
the program.