Introduction to VariablesVariables are used by programming languages to store and manipulate data contained in it. We can say that a variable is a box, that contains the data. This box, has a name, which the program calls it by its name to manipulate the data inside the variable. These variables are stored in the memory of the computer.
Variables basically consist of several things:
- Name of variable
- What type of values are stored
- Value inside the variable
Name of VariableYou can name anything as the name of the variable. In good software engineering practices, we use names that are meaningful to represent the data inside the box. Example of meaningful names to name your variable are:
NumberOfBooks, which is used to store the number of books.
Bad example:
x.
Having a name of a variable such as x or a or anything that has no meaning at all, is not recommended. Because x can be anything, and when you go back to your program and you want to make changes to it. You go like, "What the heck is x again?".
Even though you can name anything to your variable, there are certain restrictions or rules to follow:
1. Do not use keywords as variables.Keywords here are words or names that has a predefined meaning in C Language. It means that you cannot name a variable like
return because return has a meaning in C Language, which means that return a certain value back to a function. In the English Language, there are many thousands and thousands of words, and only about 40 are used as reserved keywords.
Example of keywords:
auto do goto static typedef
break double if return union
case else int short switch
char enum long long struct
const extern register signed volatile
continue float static unsigned while
default for auto void
2. No symbols are allowed in a variable
Symbols such as !@#$%^&*() are not allowed in naming a variable.
Example:
student&class //this is invalid
$var //this is invalid
3. No numbers allowed in the front of the variables
Example
1variable //this is invalid
var1able //valid
4. No spaces are allowed in variable
Since no spaces are allowed in variables, traditionally variables have underscore _ to represent spaces. Underscores are allowed to be placed in front of variables but not recommended.
Example:
student_score
Modern variables however use Uppercase to represent more than 1 word in a variable
Example:
studentScore
testOfStudents