What is Variable? What is the need for variables? What are the rules for naming variables. Give examples.
Definition of Variable
Variable is an identifier used to name the memory location which holds the value. Variable is an entity whose value can be changed (Not Fixed) during the program execution. Need for variables
It may help to think of variables as a placeholder for a value or data. You can think of a variable as being equivalent to its assigned value or data. In a C program, if a user wants to store and use any data then the user must create variables in a program to store the required data.
Rules for naming a Variable
- The variables must always begin with a letter or underscore as the first
- The following letters can be any number of letters, digits or underscore.
- Maximum length of any identifier is 31 characters for external names or 63 characters for local
- Identifiers are case Ex. Rate and RaTe are two different identifiers.
- The variable name should not be a
- No special symbols are allowed except underscore. Examples:
Valid identifiers:
Sum roll_no _name sum_of_digits avg_of_3_nums
Invalid Identifiers and reason for invalid:
$roll_no - Name doesnβt begin with either letter or underscore
for - keyword is used as name
sum,1 - special symbol comma is not allowed
3_factorial - name begins with digit as first letter