Variables
A variable is a symbolic notation associated with a memory location where the memory location can hold a value and that value can change at any time in course of the program through some statements. The way we use variables in mathematics the same way we use variables in programming. In mathematics, the volume of a cuboid is calculated using the formula v=l*b*h.
In programming also, we use the same thing. Usage of variables is similar both in mathematics and programming. So anyone with basic mathematical knowledge already knows what variables are in programming. In the above expression, ‘v’, ‘l’, ‘b’ and ‘h’ are variables.
The difference between mathematical variables
In mathematics, we write a statement (generally on a paper) and another human being may read and understand it. Generally, when we use a variable in a mathematical solution, depending on the context, the reader understands what type of data a particular variable holds. The type of data can be a whole number, a real number, a text, a character or anything else.
But in programming, a program written by us is read by the compiler (but not a human being) and the compiler cannot understand the purpose of a variable by the context. So we have to explicitly mention what type of data a variable should hold. For this, we specify a data type when creating a variable. The data type can be int, char, float or double. In C-programming, before using a variable we have to declare it.
Declaring a Variable
int x; // here ‘int’ is data type and ‘x’ is variable
double y; // here ‘double’ is data type and ‘y’ is variable
char z; // here ‘char’ is data type and ‘z’ is variable
float a,b,c; // here ‘float’ is data type and ‘a’, ‘b’ and ‘c’ are variables
What is a Variable Name
The data we work with is stored in the memory the memory area is divided into the number of bytes. Suppose, we want to store a value (say 13), that value will occupy some place in the memory. Similarly, when we store another value (suppose 10) that value will also occupy some memory location (another location) in the memory.
Each such value will occupy someplace and we need to refer those values for a number of times in the program. When we refer a location we may change the value in that location (maybe that 13 updated to 9). As the value in a location may keep changing, accessing that location with its value is difficult.
Sometimes two or more locations may have the same value and one value has no relation to the other values. In that case, it may be difficult to identify the value of which location we really wanted. To resolve this, we name each such location. This name is known as a variable name.
Features of variables
Rules applied for naming variables are also applicable to function names, structure names, union names, label names, etc.
1) A variable name is formed with one or more of the following symbols.
Lowercase alphabets: a b c d e f g h I j k l m n o p q r s t u v w x y z
Uppercase alphabets: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Digits: 0 1 2 3 4 5 6 7 8 9
Underscore: _
Eg1: area
Eg2: SIZE
Eg3: x
Eg4: m1
Eg5: gross_salary
2) A variable name cannot be started with a digit.
Eg1: first_rectangle // is valid
Eg2: 2nd_rectangle // is not valid
Eg3: 3i // is not valid
Eg4: i3 // is valid
3) There is no limit on length (number of symbols) of the variable name. But some old compilers accept only 32 characters (symbols) and ignore the remaining.
Eg1: the_date_and_time_transactions_file_is_modified_last (this variable has 52 symbols/characters in it and is acceptable on most of the systems)
4) A variable should be declared before using it.
Eg: int a=4;
printf(“%d “,a); // is valid (as a is already declared before this point)
printf(“%d “,b); // is not valid (as b is not declared before this point)
int b=5;
5) Keywords cannot be used as variables.
Eg1: int float; // is not valid
Eg2: int FLOAT; // is valid
Eg3: int float2; // is valid
Eg4: int for; // is not valid
Eg5: int else; // is not valid
Eg6: int otherwise; // is valid
Eg7: int integer; // is valid
6) When we create a variable and another variable with the same name in its inner block, then the inner variable gets priority inside the inner block.
Eg:
main()
{
int a=4;
{
int a=5;
printf(“%d “,a); // 5 is printed
}
printf(“%d “,a); // 4 is printed
}
7) A variable should be declared at beginning of a block only (on some of the compilers).
Eg:
main()
{
int a=4;
int b=5;
printf(“%d “,a);
int c=6; // not valid on some compilers
printf(“%d “,b);
{
int d=7;
int e=8;
e+=d;
int f=9; // not valid on some compilers
}
}
8) Variables cannot be created at initialization part of for loop. (in some languages like C++ and Java, variables can be created at initialization part of the for loop).
Eg:
for(int a=1;a<=3;a++) // not valid
printf(“%d “,a);
9) A variable created inside a block is known as a local variable and a variable created outside of all blocks is a global variable.
Eg:
int c=6; // c is global and can be used anywhere
void fun()
{
int b=5; // b is local to fun()
printf(“%d “,a); // not valid
printf(“%d “,b); // valid
printf(“%d “,c); // valid
}
main()
{
int a=4; // a is local to main()
fun();
printf(“%d “,a); // valid
printf(“%d “,b); // not valid
printf(“%d “,c); // valid
}
10) We cannot create two variables with a same name in a same scope/block.
Eg:
main()
{
int a=4; // a is created first time.
int a=8; // a is created second time, so NOT VALID
printf(“%d “,a);
}
11) Once we create a variable, we can modify it any number of times. Suppose we have created a variable of type ‘int’, then the keyword ‘int’ is used only when we create the variable. When using the variable later, the keyword ‘int’ is not required.
Eg:
main()
{
int a=4; // a is created
a=8; // a is modified
printf(“%d “,a); // 8 is printed
}
Different type of variables:
int a; // normal variable
int a[10]; // array variable
int *a; // pointer variable
struct set{ int x,y;} a; //structure variable