Variables in C
VARIABLES IN C
In computer terms a variable is the name of the memory location of the data which can is accessed with the help of that name and can be rewritten or used for calculations later on. Variable is a storage area. which is used to store the data. variable data is contained, which can be changed at any time during program execution.
After declaring the variable it is given a value, this value is to be assigned in the many form. Like x = 5; or a=10;
Before using the variable it is necessary to declare that you can not use the variable without declaration.
Values of variables are changeable.you can delete a value and enter another value. you can also do this on compile time and dynamically(during program execution).
Let’s say we have to store a student’s name and his roll number in variable. for this we take two variables, store the name of the student in a variable while the other variable stores the roll number.
Rules for Variable:-
- These stores the values of data types inside it.
- variable is also the name of a memory location.
- variable is case-sensitive. for example, int a or int A both are different variable.
- variable starts with any alphabet (a-z, A-Z) or underscore(_).
- variables name can be alphanumeric. for example, a1=5, var1, var2
- variable does not allow space.
- variable name does not have any C keywords.
- The name of any variable can not be started with any number.
- Any upper case and lower case character can be used in any variable name.
Datatype of Variable:
A variable should be given a type in the C language, which determines what kind of data the variable will hold. it can be:
char : it can hold a character in it.
int : it is used to hold an integer.
double : it is used to hold a double value.
float : it is used to hold a float value.
DECLARATION OF A VARIABLE
While programming you first need to tell the computer/compiler the variable’s name and it’s data type, when you do that the compiler creates an empty memory reserved for that data .
In other words, we can say that
- when the variable declare, then it allocates memory according to the data type of variable.
- After declare the variable, it takes Garbage Value inside it.
Syntax for Single Variable Declaration
printf("value of a : %d", a);
value of a : 25
Syntax for Multiple Variable Declaration
printf("value of a: %d",a);
printf("value of b: %d",b);
printf("value of c: %d",c);
value of a 25
value of b 4
value of c 67
- when the variable initialize, then it allocates the memory according to data type of variable.
- In variable initialization, a variable takes only a value.
- In variable initialization, the normal value is given the variable.
Here, a, b, c are variables and int,float,char are data types.We can also provide the value to the variable during it’s declaration.
Syntax for Single Variable Initialization
data_type single_variable_name = value;
int a = 18 ;
float b = 17.21 ;
char c = "A" ;
SCOPE OF VARIABLES
By scope of a variable we mean of part of the code a variable is accessible (visible) to .A variable can have many scopes in c let’s discuss some of them .
According to Scope, variables is divided in to two categories:-
Local variables are those variables that are defined in a small block of the program such as function, control statement block etc. Such variables are used only by the same block.
- A variable inside the function/block is a local variable .
- Local Variables is inside the function.
- The default value of the Local variables is ‘garbage value’.
int a,b,c; // local variables
b = 30;
c = a + b;
Here a, b, c all are local variables and can not be used by any other function except main .
As oppose to local variable a global variable is out side every function and is accessible to all the functions and the value of a global variable can be changed by any function.
Global variables are those variable whose scope is in whole program. These variables are defined at the beginning of the program.
- Global variables are out of the function.
- Global variables have visibility throughout the program.
- The default value of ‘Global Variables’ is ‘0’.
int d=20; // global variable
int a,b,c; // local variables
a = 10;
b = 30;
d = d + 10;
c = a + b + d;
On execution of the program the compiler prints 70 .