In C programming, variables are the most important things. Variables store the data, we use them to store the input, output, and other information. Variables are stored in memory but there are two types of variables: static variables and dynamic variables. Static variables are usually used to hold a value for a particular section of code and it will not change throughout the execution of the code. Whereas, dynamic variables will change their value every time.
Variable is the most important part of programming. It is the basic unit of any program and you can use it to perform different tasks. Variables can be either local or global. Local variables are used inside the block of the function and global variables are used outside the function. In C, there are two types of variables, dynamic and static.
Dynamic variable in C:
Dynamic variables are those which change their value depending on the condition. They are defined in the declaration section. The initial values for dynamic variables are given when the program starts running. These values remain constant and don’t change after the program runs. You can also use the term global variable to refer to the dynamic variable.
Static variable in C:
Static variable is the type of variable that does not change its value. It is used to store the value which remains constant for the whole life of the program. These variables are declared in the declaration section of the code.
Let us take a simple example to understand the scope of variables in C
int main() {
int x = 100;
x++;
printf("%d\n", x);
return 0;
}
Output: 100
Now, here is the output when we use the same code but without any variable.
main() {
printf("%d\n", x);
return 0;
}
Output: 100
So, the scope of variable is different in these two scenarios. In the first case, we can see the value of x only once, but in the second scenario, it is visible to all the sections of code.
The scope of variable refers to the place where the variable can be accessed. In other words, if you define a variable then you can access it only within the block of that function. If the variable is defined outside the block, then you can access it only in that function in Scope of Variable in C program.
Example:
void fun() {
int a, b;
a = 2;
b = 4;
printf("%d\n", a);
printf("%d\n", b);
}
In the above example, a and b are the local variables and the scope of the variables are within the function fun(). If you want to access the value of the variables in the main function then you need to define the variables as global.
Local variables are generally used to declare the data that you want to use later. Global variables are used to store data that is needed for the whole application.
Scope of variable with examples:
1. Block level:
In this case, the scope of variables is limited to a particular block of code. For example,
int main() {
int x = 100;
{
int y = 99;
printf("%d\n", x);
printf("%d\n", y);
}
printf("%d\n", x);
return 0;
}
2. Function level:
If you have any function, then the scope of variables will be restricted to that particular function.
void main() {
int x = 100;
printf("%d\n", x);
}
3. Global level:
The scope of variables is global in C. So, this means that any variable declared at the global level will be visible to all the sections of code.
int main() {
int x = 100;
printf("%d\n", x);
}
4. Unspecified:
There is no specific scope for variables, it depends on the programmer. It is possible that the programmer will define a local variable and that will be visible to all the functions within the same file.
int main() {
int x = 100;
int y = 200;
printf("%d\n", x);
printf("%d\n", y);
}
5. Data member:
A data member is a member variable of a structure and its scope is limited to the type of structure.
struct Student {
char name[20];
int roll_no;
};
6. Member of a class:
A member of a class is a member of a structure and its scope is limited to the class.
class Student {
public:
char name[20];
int roll_no
};