Variable in C
When it comes to store different data in memory and to access it for further operations it is not possible to deal with address on which data is stored, this is where Variable comes into the picture.
Variable allows us to locate data in memory. Memory address is assign to a variable while initialization and the data is stored on that location is identified by the variable.
In C programming we can change value of variable during execution of program. When we change the value of variable then it overrides the old value and store new value on same memory location.
We have to deal with various types of data during programming example integer data, float data, character data, etc.
Rules for naming variable in C
- Variable name must start with letter or underscore and not with number or any other character.
- Variables are case sensitive.
- No other special character is allowed expect underscore.
- We can’t use reserved keywords as name of a variable example if, else, switch, etc.
- Every variable must be declare first before use.
Declaration and initialization of Variables in C
We must declare variable before using it. In declaration we only declare the variable which means we deiced the type of data which variable will hold.
Memory address is not allotted at a time of declaration.
Following is the syntax and example of variable declaration.
Syntax for variable declaration.
data_type variable_name;
—————————————————
Example :
int num1;
float num2, num3; // Yes we can declare all variables of same type in single line.
We can assign value to a variable at the time of declaration and this is called as initialization.
Memory address is allotted to variable during the time of initialization.
Following is the syntax and example of variable initialization.
Syntax for variable initialization.
data_type variable_name = value;
—————————————————
Example :
int num1 = 24;
float num2 = 2.5, num3 = 8.9; // Yes we can initialized all variables of same type in single line.
Types of variable in C
There following types of variable in C
- Local Variable
- Global Variable
- Environment Variable
Local Variable
When ever we declare any variable in a function then we can’t access this variable from outside the function.
This is because scope of this variable is limited to that function. We can use this variable in that function but not out side it.
Let’s take an example.
#include<stdio.h>
void disp();
void main()
{
int num1 = 10, num2 = 20; // num1 and num2 both are local variables and their scope is limited to main()
printf(“%d”, num1+num2);
}
void disp()
{
printf(“%d”, num1+num2); // This will throw an error because num1 and num2 are not in declare in disp function
}
Global Variable
When we declare any variable before main() function i.e. in global declaration section then the scope of the variable is global.
Which means we can access this variable any where in whole program.
#include<stdio.h>
void disp();
int num1 = 10, num2 = 20; // num1 and num2 both are global variables and both the variables can be accessed from any other functions
void main()
{
printf(“%d”, num1+num2);
}
void disp()
{
printf(“%d”, num1+num2); // This will not throw any error because num1 and num2 are declared in global section
}
Environment Variable
We can access these variables any time in whole C program without even declaring them.
variables like DIR, FILE, etc. are known as environment variable.
We can change access, change and assign value of these variables with the help of following functions.
- getenv()
- setenv()
- putenv()
getenv()– As the name suggest this function is use to get value from the environment variable.
e.g. printf(“FILE : %sn”,getenv(“FILE”));
setevn()– This is use to set value in environment variable.
e.g. setenv(“FILE”,”/user/bin/basic.c”,50);
putenv()– This is use to change the value of environment variable.
e.g. putenv(“DIR=/usr/home/”);
Checkout other related topics
Practicals in C Program
Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs to understand the basic data types and I/O.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.