Variable in C
What is Variable in C ?
Variable is a name given to the memory location which is used to store the value.By default it's having garbage value.It's also having it's own unique address
or
Variable is a named block which is used to store the value.
How to declare a variable?
Syntax to declare a varible:
data_type variable_name;
The examples of declaring a variable is:
int age;
float salary;
char name;
Here, age,salary,name are variables and int,float and char are data types.
We can also assign the values to the variables at the time of declaration:
int num1=30;
float num2=10.25;
char ch='C';
Rules to Declare a variable
- Variable name should be descriptive or meaningful.
- We should not use space between the variable name,but we can use underscore(_) in place of space.
- You should not start variable name with numeric value.
- You should not use spacial characters in variable name,like(@,#,$,!,etc....).
- A variable name must not be any reserved word or keyword, e.g. int, float,char etc.
Valid Variable name are:
int num;
int num20;
int _num;
Invalid Variable name are:
int 10num; //Here, variable name begins with numeric value.
int 5; //Here, variable name is a numeric value.
int float; //Here, variable name is a keyword.
Stages of Variable
There are four stages of variable:
- Declaration e.g. int num;
- Assignment e.g. num=10;
- Initialisation e.g. int num=10;
- Accessing e.g. printf("%d",num);