What is a variable in C Language
A variable may be defined as a named memory location. The value of a variable can vary during the execution of a program.
Rules to name a variable in C Language
There are few basics rules which must be kept in mind while defining a variable. They are as follows:-
- Both digits and alphabets are allowed while naming a variable.
- Capital letters and small letters are treated differently.
- First character must be an alphabet or underscore.
- Variable name can contain digits only after first character.
- Keyword can’t be used to name a variable.
- No special character other than underscore can be used.
- It can’t contain spaces.
Examples of invalid variable names in C Language
155 | First character can’t be a digit. |
int | It is a keyword of C. |
{areaoftriangle} | Special characters other than underscore are not allowed. |
f name | Variable name can’t contain a space. |
first-name | Special character other than underscore are not allowed. |
4th | First character can’t be a digit. |
Declaration of Variables in C Language
The syntax for declaring a variable is <data-type> variable-1,variable-2,variable3;
Example:
int number;
Above statement declares a variable named number of int type.
Initialization of Variables in C Language
Initialization refers to assigning some value to a variable during declaration of variable.
Example
int a=5;